Files
gcp_solution_architecture_a…/app/tools/mcp_developer_knowledge.py
Jonathan Boniface a24a44e28c
Some checks failed
validation / verify (push) Failing after 10s
refactored: to utilise the google adk and production grade agent
2026-09-02 21:42:10 +01:00

259 lines
11 KiB
Python

"""Google Developer Knowledge MCP (Model Context Protocol) Client & Tools.
Provides integration with the Google Developer Knowledge MCP server (https://developerknowledge.googleapis.com/mcp).
Implements official MCP tools:
- developerknowledge:search_documents
- developerknowledge:get_documents
- developerknowledge:answer_query
Supports live HTTP/JSON-RPC MCP requests with built-in offline GCP knowledge fallback
for offline testing and high availability.
"""
import logging
from typing import Any, Dict, List, Optional
import httpx
from langchain_core.tools import tool
from app.config import get_settings
logger = logging.getLogger(__name__)
# Fallback Offline GCP Developer Knowledge Base
OFFLINE_GCP_KNOWLEDGE_BASE: Dict[str, Dict[str, Any]] = {
"cloud_run": {
"title": "Google Cloud Run Architecture Guide",
"uri": "https://cloud.google.com/run/docs/overview/what-is-cloud-run",
"release_status": "GA (General Availability)",
"category": "compute",
"summary": "Stateless container execution platform with automatic scaling from zero to thousands of instances, built on Knative.",
"citations": ["https://cloud.google.com/run/docs/securing/service-identity"],
},
"pubsub": {
"title": "Google Cloud Pub/Sub Messaging Best Practices",
"uri": "https://cloud.google.com/pubsub/docs/overview",
"release_status": "GA (General Availability)",
"category": "messaging",
"summary": "Globally distributed, asynchronous message bus providing at-least-once delivery with dead-letter topics and exponential backoff.",
"citations": ["https://cloud.google.com/pubsub/docs/dead-letter-topics"],
},
"storage": {
"title": "Google Cloud Storage Object Lifecycle & Security",
"uri": "https://cloud.google.com/storage/docs/overview",
"release_status": "GA (General Availability)",
"category": "storage",
"summary": "Unified object storage with uniform bucket-level access, retention lifecycle rules, customer-managed encryption (CMEK), and audit logging.",
"citations": ["https://cloud.google.com/storage/docs/uniform-bucket-level-access"],
},
"firestore": {
"title": "Google Cloud Firestore Document Database",
"uri": "https://cloud.google.com/firestore/docs/overview",
"release_status": "GA (General Availability)",
"category": "database",
"summary": "Serverless, flexible NoSQL document database built for automatic scaling, rich queries, and ACID multi-document transactions.",
"citations": ["https://cloud.google.com/firestore/docs/best-practices"],
},
"iam": {
"title": "Google Cloud IAM Least-Privilege Identity Guide",
"uri": "https://cloud.google.com/iam/docs/overview",
"release_status": "GA (General Availability)",
"category": "security",
"summary": "Fine-grained access control and least-privilege service identity management for Google Cloud resources.",
"citations": ["https://cloud.google.com/iam/docs/using-iam-securely"],
},
}
class DeveloperKnowledgeMCPClient:
"""Client for Google Developer Knowledge MCP server."""
def __init__(self, mcp_url: Optional[str] = None) -> None:
settings = get_settings()
self.mcp_url = mcp_url or settings.DEVELOPER_KNOWLEDGE_MCP_URL
self.enabled = settings.DEVELOPER_KNOWLEDGE_MCP_ENABLED
self.timeout = httpx.Timeout(5.0)
def search_documents(self, query: str, category: str = "all") -> Dict[str, Any]:
"""Search Google Cloud reference architecture, decision-making, and best-practice documents."""
if self.enabled and self.mcp_url:
try:
payload = {
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "developerknowledge:search_documents",
"arguments": {"query": query, "category": category},
},
"id": 1,
}
with httpx.Client(timeout=self.timeout) as client:
resp = client.post(self.mcp_url, json=payload)
if resp.status_code == 200:
data = resp.json()
if "result" in data:
return {"source": "live_mcp", "data": data["result"]}
except Exception as exc:
logger.debug("Live MCP search_documents query failed (%s); using offline knowledge base.", exc)
# Offline fallback search
results = []
q_lower = query.lower()
for key, doc in OFFLINE_GCP_KNOWLEDGE_BASE.items():
if (
q_lower in doc["title"].lower()
or q_lower in doc["summary"].lower()
or q_lower in doc["category"].lower()
or category == "all"
or category == doc["category"]
):
results.append(doc)
return {
"source": "offline_fallback",
"query": query,
"category": category,
"total_matches": len(results),
"documents": results,
}
def get_documents(self, document_uri: str) -> Dict[str, Any]:
"""Fetch official Google Cloud document content and citations by URI."""
if self.enabled and self.mcp_url:
try:
payload = {
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "developerknowledge:get_documents",
"arguments": {"document_uri": document_uri},
},
"id": 2,
}
with httpx.Client(timeout=self.timeout) as client:
resp = client.post(self.mcp_url, json=payload)
if resp.status_code == 200:
data = resp.json()
if "result" in data:
return {"source": "live_mcp", "data": data["result"]}
except Exception as exc:
logger.debug("Live MCP get_documents query failed (%s); using offline knowledge base.", exc)
# Search matching offline doc
for doc in OFFLINE_GCP_KNOWLEDGE_BASE.values():
if document_uri in doc["uri"] or doc["uri"] in document_uri:
return {"source": "offline_fallback", "document": doc}
return {
"source": "offline_fallback",
"document_uri": document_uri,
"document": {
"title": f"Google Cloud Documentation ({document_uri})",
"uri": document_uri,
"release_status": "GA",
"summary": "Official Google Cloud architecture reference documentation.",
"citations": [document_uri],
},
}
def answer_query(self, query: str) -> Dict[str, Any]:
"""Answer architectural questions and check GCP product release statuses and best practices."""
if self.enabled and self.mcp_url:
try:
payload = {
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "developerknowledge:answer_query",
"arguments": {"query": query},
},
"id": 3,
}
with httpx.Client(timeout=self.timeout) as client:
resp = client.post(self.mcp_url, json=payload)
if resp.status_code == 200:
data = resp.json()
if "result" in data:
return {"source": "live_mcp", "answer": data["result"]}
except Exception as exc:
logger.debug("Live MCP answer_query failed (%s); using offline knowledge base.", exc)
# Offline grounding logic
q_lower = query.lower()
if "release status" in q_lower or "deprecated" in q_lower:
return {
"source": "offline_fallback",
"query": query,
"status_check": "All recommended products (Cloud Run, Pub/Sub, Cloud Storage, Firestore, Cloud IAM) are Active GA (General Availability). None are deprecated.",
"supported": True,
}
return {
"source": "offline_fallback",
"query": query,
"answer": (
"Google Cloud Architecture Best Practice: Design regional event-driven workloads "
"using Cloud Run for stateless container execution, Pub/Sub for asynchronous message buffering, "
"and Cloud Storage / Firestore for durable state retention under least-privilege IAM."
),
"citations": ["https://cloud.google.com/architecture/framework"],
}
# Singleton client instance
_mcp_client: Optional[DeveloperKnowledgeMCPClient] = None
def get_mcp_client() -> DeveloperKnowledgeMCPClient:
"""Get singleton DeveloperKnowledgeMCPClient."""
global _mcp_client
if _mcp_client is None:
_mcp_client = DeveloperKnowledgeMCPClient()
return _mcp_client
# ---------------------------------------------------------------------------
# LangChain @tool wrappers matching official Google spec
# ---------------------------------------------------------------------------
@tool
def developerknowledge_search_documents(query: str, category: str = "all") -> Dict[str, Any]:
"""Searches Google Cloud reference architecture, decision-making, and best-practice documents.
Args:
query: Search query string (e.g. 'Cloud Run PubSub event architecture').
category: Optional category filter ('compute', 'messaging', 'storage', 'security', or 'all').
Returns:
Dict containing matching Google Cloud documents and citations.
"""
client = get_mcp_client()
return client.search_documents(query=query, category=category)
@tool
def developerknowledge_get_documents(document_uri: str) -> Dict[str, Any]:
"""Retrieves official Google Cloud document content and citations by URI.
Args:
document_uri: Official Google Cloud documentation URL or document identifier.
Returns:
Dict containing document metadata, summary, and citations.
"""
client = get_mcp_client()
return client.get_documents(document_uri=document_uri)
@tool
def developerknowledge_answer_query(query: str) -> Dict[str, Any]:
"""Answers architectural questions and checks GCP product release statuses and best practices.
Args:
query: Architectural question or product status query string.
Returns:
Dict containing grounded answer, release status, and documentation citations.
"""
client = get_mcp_client()
return client.answer_query(query=query)