-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathcoordinator.py
More file actions
161 lines (136 loc) · 5.4 KB
/
coordinator.py
File metadata and controls
161 lines (136 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Copyright 2025 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Module declaring the singleton MCP server.
The singleton allows other modules to register their tools with the same MCP
server.
"""
# MCP Server Imports
import json
from mcp import types as mcp_types # Use alias to avoid conflict
from mcp.server.lowlevel import Server
# ADK Tool Imports
from google.adk.tools.function_tool import FunctionTool
from google.adk.tools.mcp_tool.conversion_utils import adk_to_mcp_tool_type
from analytics_mcp.tools.admin.info import (
get_account_summaries,
list_google_ads_links,
get_property_details,
list_property_annotations,
)
from analytics_mcp.tools.reporting.core import (
run_report,
_run_report_description,
)
from analytics_mcp.tools.reporting.realtime import (
run_realtime_report,
_run_realtime_report_description,
)
from analytics_mcp.tools.reporting.compatibility import (
check_compatibility,
)
from analytics_mcp.tools.reporting.metadata import (
get_custom_dimensions_and_metrics,
)
run_report_with_description = FunctionTool(run_report)
run_report_with_description.description = _run_report_description()
run_realtime_report_with_description = FunctionTool(run_realtime_report)
run_realtime_report_with_description.description = (
_run_realtime_report_description()
)
# Instantiate the ADK tools
tools = [
FunctionTool(get_account_summaries),
FunctionTool(list_google_ads_links),
FunctionTool(get_property_details),
FunctionTool(list_property_annotations),
FunctionTool(get_custom_dimensions_and_metrics),
run_report_with_description,
run_realtime_report_with_description,
FunctionTool(check_compatibility),
]
tool_map = {t.name: t for t in tools}
app = Server(
name="Google Analytics MCP Server",
)
mcp_tools = [adk_to_mcp_tool_type(tool) for tool in tools]
def sanitize_mcp_schema_properties(node: dict) -> None:
"""Ensure additionalProperties is a boolean value to satisfy certain MCP clients.
This addresses issues with clients like Claude Desktop that fail when
additionalProperties is a schema object instead of a boolean.
"""
if not isinstance(node, dict):
return
# Check and update the current node
if "additionalProperties" in node:
val = node["additionalProperties"]
if not isinstance(val, bool):
node["additionalProperties"] = True
# Traverse children
for key, child in node.items():
if isinstance(child, dict):
sanitize_mcp_schema_properties(child)
elif isinstance(child, list):
for element in child:
if isinstance(element, dict):
sanitize_mcp_schema_properties(element)
# Update the inputSchema for tools that do not have parameters.
# TODO: This is a bug in the ADK and can be removed once it is fixed.
# https://github.com/google/adk-python/issues/948
for tool in mcp_tools:
# Check if inputSchema is empty
if tool.inputSchema == {}:
tool.inputSchema = {"type": "object", "properties": {}}
# Fix union type hints generating spurious "type": "null"
for prop in tool.inputSchema.get("properties", {}).values():
if "anyOf" in prop and prop.get("type") == "null":
del prop["type"]
# Ensure additionalProperties is compatible with all MCP clients
sanitize_mcp_schema_properties(tool.inputSchema)
# Explicitly mark required fields for reporting tools to guide the LLM
if tool.name == "run_report":
tool.inputSchema["required"] = [
"property_id",
"date_ranges",
"dimensions",
"metrics",
]
elif tool.name == "run_realtime_report":
tool.inputSchema["required"] = ["property_id", "dimensions", "metrics"]
@app.list_tools()
async def list_tools() -> list[mcp_types.Tool]:
return mcp_tools
@app.call_tool()
async def call_mcp_tool(name: str, arguments: dict) -> list[mcp_types.Content]:
if name in tool_map:
tool = tool_map[name]
try:
adk_tool_response = await tool.run_async(
args=arguments,
tool_context=None,
)
# Serialize the ADK tool response to JSON for MCP response
response_text = json.dumps(adk_tool_response, indent=2)
# MCP expects a list of mcp_types.Content parts
return [mcp_types.TextContent(type="text", text=response_text)]
except Exception as e:
print(f"MCP Server: Error executing ADK tool '{name}': {e}")
# Return an error message in MCP format
error_text = json.dumps(
{"error": f"Failed to execute tool '{name}': {str(e)}"}
)
return [mcp_types.TextContent(type="text", text=error_text)]
error_text = json.dumps(
{"error": f"Tool '{name}' not implemented by this server."}
)
return [mcp_types.TextContent(type="text", text=error_text)]