-
-
Notifications
You must be signed in to change notification settings - Fork 919
Expand file tree
/
Copy pathcli_handler.py
More file actions
501 lines (405 loc) · 15.4 KB
/
Copy pathcli_handler.py
File metadata and controls
501 lines (405 loc) · 15.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
"""
CLI Handler for Google Workspace MCP
This module provides a command-line interface mode for directly invoking
MCP tools without running the full server. Designed for use by coding agents
(Codex, Claude Code) and command-line users.
Usage:
workspace-mcp --cli # List available tools
workspace-mcp --cli list # List available tools
workspace-mcp --cli <tool_name> # Run tool (reads JSON args from stdin)
workspace-mcp --cli <tool_name> --args '{"key": "value"}' # Run with inline args
workspace-mcp --cli <tool_name> --help # Show tool details
"""
import asyncio
import inspect
import json
import logging
import sys
from typing import Any, Dict, List, Optional
from auth.oauth_config import set_transport_mode
logger = logging.getLogger(__name__)
def _is_fastapi_param_marker(default: Any) -> bool:
"""
Check if a default value is a FastAPI parameter marker (Body, Query, etc.).
These markers are metadata for HTTP request parsing and should not be passed
directly to tool functions in CLI mode.
"""
default_type = type(default)
return default_type.__module__ == "fastapi.params" and hasattr(
default, "get_default"
)
def _is_required_marker_default(value: Any) -> bool:
"""Check whether a FastAPI/Pydantic default represents a required field."""
return value is Ellipsis or type(value).__name__ == "PydanticUndefinedType"
def _extract_fastapi_default(default_marker: Any) -> tuple[bool, Any]:
"""
Resolve the runtime default from a FastAPI marker.
Returns:
Tuple of (is_required, resolved_default)
"""
try:
resolved_default = default_marker.get_default(call_default_factory=True)
except TypeError:
# Compatibility path for implementations without call_default_factory kwarg
resolved_default = default_marker.get_default()
except Exception:
resolved_default = getattr(default_marker, "default", inspect.Parameter.empty)
return _is_required_marker_default(resolved_default), resolved_default
def _normalize_cli_args_for_tool(fn, args: Dict[str, Any]) -> Dict[str, Any]:
"""
Fill omitted CLI args for FastAPI markers with their real defaults.
When tools are invoked via HTTP, FastAPI resolves Body/Query/... defaults.
In CLI mode we invoke functions directly, so we need to do that resolution.
"""
normalized_args = dict(args)
signature = inspect.signature(fn)
missing_required = []
for param in signature.parameters.values():
if param.kind in (
inspect.Parameter.VAR_POSITIONAL,
inspect.Parameter.VAR_KEYWORD,
):
continue
if param.name in normalized_args:
continue
if param.default is inspect.Parameter.empty:
continue
if not _is_fastapi_param_marker(param.default):
continue
is_required, resolved_default = _extract_fastapi_default(param.default)
if is_required:
missing_required.append(param.name)
else:
normalized_args[param.name] = resolved_default
if missing_required:
if len(missing_required) == 1:
missing = missing_required[0]
raise TypeError(
f"{fn.__name__}() missing 1 required positional argument: '{missing}'"
)
missing_names = [f"'{name}'" for name in missing_required]
if len(missing_names) == 2:
missing = " and ".join(missing_names)
else:
missing = ", ".join(missing_names[:-1]) + f" and {missing_names[-1]}"
raise TypeError(
f"{fn.__name__}() missing {len(missing_required)} required positional arguments: {missing}"
)
return normalized_args
def get_registered_tools(server) -> Dict[str, Any]:
"""
Get all registered tools from the FastMCP server.
Args:
server: The FastMCP server instance
Returns:
Dictionary mapping tool names to their metadata
"""
tools = {}
if hasattr(server, "_tool_manager") and hasattr(server._tool_manager, "_tools"):
tool_registry = server._tool_manager._tools
for name, tool in tool_registry.items():
tools[name] = {
"name": name,
"description": getattr(tool, "description", None)
or _extract_docstring(tool),
"parameters": _extract_parameters(tool),
"tool_obj": tool,
}
return tools
def _extract_docstring(tool) -> Optional[str]:
"""Extract the first meaningful line of a tool's docstring as its description."""
fn = getattr(tool, "fn", None) or tool
if fn and fn.__doc__:
# Get first non-empty line that's not just "Args:" etc.
for line in fn.__doc__.strip().split("\n"):
line = line.strip()
# Skip empty lines and common section headers
if line and not line.startswith(
("Args:", "Returns:", "Raises:", "Example", "Note:")
):
return line
return None
def _extract_parameters(tool) -> Dict[str, Any]:
"""Extract parameter information from a tool."""
params = {}
# Try to get parameters from the tool's schema
if hasattr(tool, "parameters"):
schema = tool.parameters
if isinstance(schema, dict):
props = schema.get("properties", {})
required = set(schema.get("required", []))
for name, prop in props.items():
params[name] = {
"type": prop.get("type", "any"),
"description": prop.get("description", ""),
"required": name in required,
"default": prop.get("default"),
}
return params
def list_tools(server, output_format: str = "text") -> str:
"""
List all available tools.
Args:
server: The FastMCP server instance
output_format: Output format ("text" or "json")
Returns:
Formatted string listing all tools
"""
tools = get_registered_tools(server)
if output_format == "json":
# Return JSON format for programmatic use
tool_list = []
for name, info in sorted(tools.items()):
tool_list.append(
{
"name": name,
"description": info["description"],
"parameters": info["parameters"],
}
)
return json.dumps({"tools": tool_list}, indent=2)
# Text format for human reading
lines = [
f"Available tools ({len(tools)}):",
"",
]
# Group tools by service
services = {}
for name, info in tools.items():
# Extract service prefix from tool name
prefix = name.split("_")[0] if "_" in name else "other"
if prefix not in services:
services[prefix] = []
services[prefix].append((name, info))
for service in sorted(services.keys()):
lines.append(f" {service.upper()}:")
for name, info in sorted(services[service]):
desc = info["description"] or "(no description)"
# Get first line only and truncate
first_line = desc.split("\n")[0].strip()
if len(first_line) > 70:
first_line = first_line[:67] + "..."
lines.append(f" {name}")
lines.append(f" {first_line}")
lines.append("")
lines.append("Use --cli <tool_name> --help for detailed tool information")
lines.append("Use --cli <tool_name> --args '{...}' to run a tool")
return "\n".join(lines)
def show_tool_help(server, tool_name: str) -> str:
"""
Show detailed help for a specific tool.
Args:
server: The FastMCP server instance
tool_name: Name of the tool
Returns:
Formatted help string for the tool
"""
tools = get_registered_tools(server)
if tool_name not in tools:
available = ", ".join(sorted(tools.keys())[:10])
return f"Error: Tool '{tool_name}' not found.\n\nAvailable tools include: {available}..."
tool_info = tools[tool_name]
tool_obj = tool_info["tool_obj"]
# Get full docstring
fn = getattr(tool_obj, "fn", None) or tool_obj
docstring = fn.__doc__ if fn and fn.__doc__ else "(no documentation)"
lines = [
f"Tool: {tool_name}",
"=" * (len(tool_name) + 6),
"",
docstring,
"",
"Parameters:",
]
params = tool_info["parameters"]
if params:
for name, param_info in params.items():
req = "(required)" if param_info.get("required") else "(optional)"
param_type = param_info.get("type", "any")
desc = param_info.get("description", "")
default = param_info.get("default")
lines.append(f" {name}: {param_type} {req}")
if desc:
lines.append(f" {desc}")
if default is not None:
lines.append(f" Default: {default}")
else:
lines.append(" (no parameters)")
lines.extend(
[
"",
"Example usage:",
f' workspace-mcp --cli {tool_name} --args \'{{"param": "value"}}\'',
"",
"Or pipe JSON from stdin:",
f' echo \'{{"param": "value"}}\' | workspace-mcp --cli {tool_name}',
]
)
return "\n".join(lines)
async def run_tool(server, tool_name: str, args: Dict[str, Any]) -> str:
"""
Execute a tool with the provided arguments.
Args:
server: The FastMCP server instance
tool_name: Name of the tool to execute
args: Dictionary of arguments to pass to the tool
Returns:
Tool result as a string
"""
tools = get_registered_tools(server)
if tool_name not in tools:
raise ValueError(f"Tool '{tool_name}' not found")
tool_info = tools[tool_name]
tool_obj = tool_info["tool_obj"]
# Get the actual function to call
fn = getattr(tool_obj, "fn", None)
if fn is None:
raise ValueError(f"Tool '{tool_name}' has no callable function")
call_args = dict(args)
try:
call_args = _normalize_cli_args_for_tool(fn, args)
logger.debug(
f"[CLI] Executing tool: {tool_name} with args: {list(call_args.keys())}"
)
# Call the tool function
if asyncio.iscoroutinefunction(fn):
result = await fn(**call_args)
else:
result = fn(**call_args)
# Convert result to string if needed
if isinstance(result, str):
return result
else:
return json.dumps(result, indent=2, default=str)
except TypeError as e:
# Provide helpful error for missing/invalid arguments
error_msg = str(e)
params = tool_info["parameters"]
required = [n for n, p in params.items() if p.get("required")]
return (
f"Error calling {tool_name}: {error_msg}\n\n"
f"Required parameters: {required}\n"
f"Provided parameters: {list(call_args.keys())}"
)
except Exception as e:
logger.error(f"[CLI] Error executing {tool_name}: {e}", exc_info=True)
return f"Error: {type(e).__name__}: {e}"
def parse_cli_args(args: List[str]) -> Dict[str, Any]:
"""
Parse CLI arguments for tool execution.
Args:
args: List of arguments after --cli
Returns:
Dictionary with parsed values:
- command: "list", "help", or "run"
- tool_name: Name of tool (if applicable)
- tool_args: Arguments for the tool (if applicable)
- output_format: "text" or "json"
"""
result = {
"command": "list",
"tool_name": None,
"tool_args": {},
"output_format": "text",
}
if not args:
return result
i = 0
while i < len(args):
arg = args[i]
if arg in ("list", "-l", "--list"):
result["command"] = "list"
i += 1
elif arg in ("--json", "-j"):
result["output_format"] = "json"
i += 1
elif arg in ("help", "--help", "-h"):
# Help command - if tool_name already set, show help for that tool
if result["tool_name"]:
result["command"] = "help"
else:
# Check if next arg is a tool name
if i + 1 < len(args) and not args[i + 1].startswith("-"):
result["tool_name"] = args[i + 1]
result["command"] = "help"
i += 1
else:
# No tool specified, show general help
result["command"] = "list"
i += 1
elif arg in ("--args", "-a") and i + 1 < len(args):
# Parse inline JSON arguments
json_str = args[i + 1]
try:
result["tool_args"] = json.loads(json_str)
except json.JSONDecodeError as e:
# Provide helpful debug info
raise ValueError(
f"Invalid JSON in --args: {e}\n"
f"Received: {repr(json_str)}\n"
f"Tip: Try using stdin instead: echo '<json>' | workspace-mcp --cli <tool>"
)
i += 2
elif not arg.startswith("-") and not result["tool_name"]:
# First non-flag argument is the tool name
result["tool_name"] = arg
result["command"] = "run"
i += 1
else:
i += 1
return result
def read_stdin_args() -> Dict[str, Any]:
"""
Read JSON arguments from stdin if available.
Returns:
Dictionary of arguments or empty dict if stdin is a TTY or no data is provided.
"""
if sys.stdin.isatty():
logger.debug("[CLI] stdin is a TTY; no JSON args will be read from stdin")
return {}
try:
stdin_data = sys.stdin.read().strip()
if stdin_data:
return json.loads(stdin_data)
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON from stdin: {e}")
return {}
async def handle_cli_mode(server, cli_args: List[str]) -> int:
"""
Main entry point for CLI mode.
Args:
server: The FastMCP server instance
cli_args: Arguments passed after --cli
Returns:
Exit code (0 for success, 1 for error)
"""
# Set transport mode to "stdio" so OAuth callback server starts when needed
# This is required for authentication flow when no cached credentials exist
set_transport_mode("stdio")
try:
parsed = parse_cli_args(cli_args)
if parsed["command"] == "list":
output = list_tools(server, parsed["output_format"])
print(output)
return 0
if parsed["command"] == "help":
output = show_tool_help(server, parsed["tool_name"])
print(output)
return 0
if parsed["command"] == "run":
# Merge stdin args with inline args (inline takes precedence)
args = read_stdin_args()
args.update(parsed["tool_args"])
result = await run_tool(server, parsed["tool_name"], args)
print(result)
return 0
# Unknown command
print(f"Unknown command: {parsed['command']}")
return 1
except ValueError as e:
print(f"Error: {e}", file=sys.stderr)
return 1
except Exception as e:
logger.error(f"[CLI] Unexpected error: {e}", exc_info=True)
print(f"Error: {e}", file=sys.stderr)
return 1