-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
325 lines (282 loc) · 8.76 KB
/
mcp_server.py
File metadata and controls
325 lines (282 loc) · 8.76 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python3
"""MCP server exposing agent-friend's built-in tools via the Model Context Protocol.
Usage:
agent-friend-mcp # recommended (installed entry point)
python mcp_server.py # direct run from repo clone
Claude Desktop config (~/.claude/claude_desktop_config.json):
{
"mcpServers": {
"agent-friend": {
"command": "agent-friend-mcp"
}
}
}
All agent-friend tools that can be instantiated without arguments are
registered automatically. Each tool method becomes a separate MCP tool
with the naming convention: {tool_name}_{method_name} (e.g. "datetime_now",
"crypto_hash_data", "json_get").
Tool count: ~310 methods from ~50 tool classes.
"""
import json
import sys
from pathlib import Path
from typing import Any, Dict, List, Optional
# Ensure agent-friend is importable when running directly from repo
_project_root = str(Path(__file__).resolve().parent)
if _project_root not in sys.path:
sys.path.insert(0, _project_root)
from mcp.server.fastmcp import FastMCP
from agent_friend.tools import (
AlertTool,
AuditTool,
BatchTool,
BrowserTool,
CacheTool,
ChunkerTool,
CodeTool,
ConfigTool,
CryptoTool,
DatabaseTool,
DateTimeTool,
DiffTool,
EnvTool,
EventBusTool,
FetchTool,
FileTool,
FormatTool,
GitTool,
GraphTool,
HTMLTool,
HTTPTool,
JSONTool,
LockTool,
MapReduceTool,
MemoryTool,
MetricsTool,
NotifyTool,
ProcessTool,
QueueTool,
RateLimitTool,
RSSFeedTool,
RegexTool,
RetryTool,
SamplerTool,
SchedulerTool,
SearchIndexTool,
SearchTool,
StateMachineTool,
StatsTool,
TableTool,
TemplateTool,
TimerTool,
TransformTool,
ValidatorTool,
VectorStoreTool,
VoiceTool,
WebhookTool,
WorkflowTool,
XMLTool,
)
# ---------------------------------------------------------------------------
# All tool classes to register (those that take no constructor args)
# ---------------------------------------------------------------------------
TOOL_CLASSES = [
AlertTool,
AuditTool,
BatchTool,
BrowserTool,
CacheTool,
ChunkerTool,
CodeTool,
ConfigTool,
CryptoTool,
DatabaseTool,
DateTimeTool,
DiffTool,
EnvTool,
EventBusTool,
FetchTool,
FileTool,
FormatTool,
GitTool,
GraphTool,
HTMLTool,
HTTPTool,
JSONTool,
LockTool,
MapReduceTool,
MemoryTool,
MetricsTool,
NotifyTool,
ProcessTool,
QueueTool,
RateLimitTool,
RSSFeedTool,
RegexTool,
RetryTool,
SamplerTool,
SchedulerTool,
SearchIndexTool,
SearchTool,
StateMachineTool,
StatsTool,
TableTool,
TemplateTool,
TimerTool,
TransformTool,
ValidatorTool,
VectorStoreTool,
VoiceTool,
WebhookTool,
WorkflowTool,
XMLTool,
]
# ---------------------------------------------------------------------------
# JSON Schema type -> Python type string mapping
# ---------------------------------------------------------------------------
_TYPE_MAP = {
"string": "str",
"integer": "int",
"number": "float",
"boolean": "bool",
"array": "list",
"object": "dict",
}
# Python keywords that cannot be used as parameter names
_PYTHON_KEYWORDS = {
"and", "as", "assert", "async", "await", "break", "class", "continue",
"def", "del", "elif", "else", "except", "finally", "for", "from",
"global", "if", "import", "in", "is", "lambda", "nonlocal", "not",
"or", "pass", "raise", "return", "try", "while", "with", "yield",
}
def _resolve_type(prop: Dict[str, Any]) -> str:
"""Resolve a JSON Schema property to a Python type string."""
t = prop.get("type")
if t is None:
# No type specified -> accept anything as str
return "str"
if isinstance(t, list):
# Union type like ["string", "null"] -> use first non-null
for sub in t:
if sub != "null":
return _TYPE_MAP.get(sub, "str")
return "str"
return _TYPE_MAP.get(t, "str")
def _safe_param_name(name: str) -> str:
"""Ensure a parameter name is a valid Python identifier and not a keyword."""
if name in _PYTHON_KEYWORDS:
return f"{name}_"
if not name.isidentifier():
return name.replace("-", "_").replace(".", "_")
return name
def _make_mcp_handler(tool_instance, method_name: str, input_schema: Dict[str, Any]):
"""Dynamically create a typed Python function that dispatches to
tool_instance.execute(method_name, arguments).
FastMCP inspects function signatures to build MCP input schemas, so
we create functions with proper type annotations matching the tool's
JSON Schema definition.
"""
props = input_schema.get("properties", {})
required = set(input_schema.get("required", []))
if not props:
# No parameters -- simple handler
def handler() -> str:
result = tool_instance.execute(method_name, {})
return result if isinstance(result, str) else json.dumps(result)
return handler
# Build parameter list for the function signature
# Required params first, then optional ones
req_params = []
opt_params = []
# Track original names if we had to rename
renames = {}
for pname, prop in props.items():
py_type = _resolve_type(prop)
safe_name = _safe_param_name(pname)
if safe_name != pname:
renames[safe_name] = pname
if pname in required:
req_params.append(f"{safe_name}: {py_type}")
else:
opt_params.append(f"{safe_name}: Optional[{py_type}] = None")
param_str = ", ".join(req_params + opt_params)
# Build the function body
# Collect all params into a dict, stripping None values
func_code = f"""
def _handler({param_str}) -> str:
_args = {{k: v for k, v in locals().items() if v is not None}}
# Restore original param names if renamed
for _safe, _orig in _renames.items():
if _safe in _args:
_args[_orig] = _args.pop(_safe)
return _dispatch(_args)
"""
namespace = {
"_dispatch": lambda args: _dispatch_call(tool_instance, method_name, args),
"_renames": renames,
"Optional": Optional,
}
exec(func_code, namespace)
return namespace["_handler"]
def _dispatch_call(tool_instance, method_name: str, args: Dict[str, Any]) -> str:
"""Call tool_instance.execute and ensure the result is a string."""
result = tool_instance.execute(method_name, args)
if not isinstance(result, str):
return json.dumps(result)
return result
# ---------------------------------------------------------------------------
# Initialize FastMCP server and register tools
# ---------------------------------------------------------------------------
server = FastMCP(
"agent-friend",
instructions=(
"agent-friend tools: a collection of zero-dependency Python utilities "
"covering date/time, crypto, validation, JSON manipulation, regex, "
"formatting, diffing, text processing, data structures, and more. "
"Each tool is prefixed with its category (e.g. datetime_, crypto_, json_)."
),
)
registered_count = 0
skipped_classes = []
seen_names: set = set()
for cls in TOOL_CLASSES:
try:
tool_inst = cls()
except TypeError as e:
skipped_classes.append((cls.__name__, str(e)))
continue
tool_prefix = tool_inst.name
for defn in tool_inst.definitions():
method_name = defn["name"]
description = defn.get("description", "")
input_schema = defn.get("input_schema", {"type": "object", "properties": {}})
# Unique MCP tool name: {category}_{method}
mcp_name = f"{tool_prefix}_{method_name}"
if mcp_name in seen_names:
mcp_name = f"{mcp_name}_2"
seen_names.add(mcp_name)
# Build a typed handler function
handler = _make_mcp_handler(tool_inst, method_name, input_schema)
handler.__name__ = mcp_name
handler.__qualname__ = mcp_name
handler.__doc__ = f"[{tool_prefix}] {description}"
# Register with FastMCP
server.add_tool(
handler,
name=mcp_name,
description=f"[{tool_prefix}] {description}",
)
registered_count += 1
print(
f"agent-friend MCP server: registered {registered_count} tools "
f"from {len(TOOL_CLASSES) - len(skipped_classes)} tool classes",
file=sys.stderr,
)
if skipped_classes:
for name, reason in skipped_classes:
print(f" skipped {name}: {reason}", file=sys.stderr)
# ---------------------------------------------------------------------------
# Run
# ---------------------------------------------------------------------------
if __name__ == "__main__":
server.run(transport="stdio")