|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +import logging |
| 4 | +import shutil |
| 5 | +from dataclasses import dataclass |
| 6 | + |
| 7 | +import click |
| 8 | +import pyperclip |
| 9 | +from mcp import ClientSession |
| 10 | +from mcp import StdioServerParameters |
| 11 | +from mcp.client.stdio import stdio_client |
| 12 | + |
| 13 | +logging.basicConfig(level=logging.INFO) |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class InputParameter: |
| 18 | + name: str |
| 19 | + type: str |
| 20 | + required: bool |
| 21 | + key: str |
| 22 | + description: str |
| 23 | + |
| 24 | + |
| 25 | +def find_input_tokens(data): |
| 26 | + tokens = set() |
| 27 | + if isinstance(data, list): |
| 28 | + for item in data: |
| 29 | + tokens.update(find_input_tokens(item)) |
| 30 | + elif isinstance(data, dict): |
| 31 | + for value in data.values(): |
| 32 | + tokens.update(find_input_tokens(value)) |
| 33 | + elif isinstance(data, str) and data.startswith("${input:"): |
| 34 | + tokens.add(data[8:-1].strip()) |
| 35 | + return tokens |
| 36 | + |
| 37 | + |
| 38 | +# New mcp group |
| 39 | +@click.group() |
| 40 | +def mcp(): |
| 41 | + """mcp specific commands.""" |
| 42 | + |
| 43 | + |
| 44 | +@mcp.command("inspect-mcp-server") |
| 45 | +def create_mcp_proxy(): |
| 46 | + content = click.edit() |
| 47 | + if content is None: |
| 48 | + click.echo("No input provided.") |
| 49 | + return |
| 50 | + |
| 51 | + try: |
| 52 | + config = json.loads(content) |
| 53 | + except json.JSONDecodeError: |
| 54 | + click.echo("Invalid JSON content.") |
| 55 | + return |
| 56 | + |
| 57 | + inputs = {} |
| 58 | + mcp_config = config.get("mcp", {}) |
| 59 | + |
| 60 | + # Select server |
| 61 | + # Support both "servers" and "mcpServers" naming conventions |
| 62 | + servers = mcp_config.get("mcpServers", mcp_config.get("servers", {})) |
| 63 | + server_names = list(servers.keys()) |
| 64 | + |
| 65 | + if not server_names: |
| 66 | + ctx = click.get_current_context() |
| 67 | + click.secho("Error: No servers configured in mcp config (tried keys: 'mcpServers' and 'servers')", fg="red") |
| 68 | + ctx.exit(1) |
| 69 | + |
| 70 | + if len(server_names) > 1: |
| 71 | + server_name = click.prompt("Choose a server", type=click.Choice(server_names), show_choices=True) |
| 72 | + else: |
| 73 | + server_name = server_names[0] |
| 74 | + |
| 75 | + if server_name in servers: |
| 76 | + server_config = servers[server_name] |
| 77 | + |
| 78 | + # Collect input tokens ONLY from this server's config |
| 79 | + input_ids = find_input_tokens(server_config.get("args", [])) |
| 80 | + input_ids.update(find_input_tokens(server_config.get("env", {}))) |
| 81 | + |
| 82 | + # Create prompt definitions using BOTH discovered tokens AND configured inputs |
| 83 | + existing_input_ids = {i["id"] for i in mcp_config.get("inputs", [])} |
| 84 | + inputs_to_prompt = input_ids.intersection(existing_input_ids) |
| 85 | + inputs_to_prompt.update(input_ids) # Add any undiscovered-by-config inputs |
| 86 | + |
| 87 | + input_configs = [] |
| 88 | + for input_id in inputs_to_prompt: |
| 89 | + input_def = next((d for d in mcp_config.get("inputs", []) if d["id"] == input_id), {}) |
| 90 | + inputs[input_id] = click.prompt( |
| 91 | + input_def.get("description", input_id), |
| 92 | + hide_input=True, |
| 93 | + ) |
| 94 | + # Create InputParameters config entry |
| 95 | + input_configs.append( |
| 96 | + InputParameter( |
| 97 | + name=input_def.get("name", input_id), |
| 98 | + type="password", |
| 99 | + required=True, |
| 100 | + key=input_id, |
| 101 | + description=input_def.get("description", ""), |
| 102 | + ).__dict__ |
| 103 | + ) |
| 104 | + |
| 105 | + # Replace input tokens in args |
| 106 | + processed_args = [ |
| 107 | + inputs.get(arg[8:-1], arg) if isinstance(arg, str) and arg.startswith("${input:") else arg |
| 108 | + for arg in server_config.get("args", []) |
| 109 | + ] |
| 110 | + |
| 111 | + # Replace input tokens in environment variables |
| 112 | + processed_env = { |
| 113 | + k: inputs.get(v[8:-1], v) if isinstance(v, str) and v.startswith("${input:") else v |
| 114 | + for k, v in server_config.get("env", {}).items() |
| 115 | + } |
| 116 | + |
| 117 | + # Execute with processed parameters |
| 118 | + output = asyncio.run( |
| 119 | + list_tools(server_config=server_config, command=server_config["command"], args=processed_args, env=processed_env) |
| 120 | + ) |
| 121 | + # Add processed parameters to output |
| 122 | + output_with_name = { |
| 123 | + "name": server_name, |
| 124 | + "config": input_configs, |
| 125 | + "command": server_config["command"], |
| 126 | + "args": [arg.replace("${input:", "${") if isinstance(arg, str) else arg for arg in server_config.get("args", [])], |
| 127 | + "env": [ |
| 128 | + {"key": k, "value": v.replace("${input:", "${") if isinstance(v, str) else v} |
| 129 | + for k, v in server_config.get("env", {}).items() |
| 130 | + ], |
| 131 | + **output, |
| 132 | + } |
| 133 | + output_json = json.dumps(output_with_name, indent=2) |
| 134 | + click.echo(output_json) |
| 135 | + try: |
| 136 | + pyperclip.copy(output_json) |
| 137 | + click.secho("\nOutput copied to clipboard!", fg="green") |
| 138 | + except pyperclip.PyperclipException as e: |
| 139 | + click.secho(f"\nFailed to copy to clipboard: {e!s}", fg="yellow") |
| 140 | + |
| 141 | + |
| 142 | +async def list_tools(server_config: dict, command: str, args: list[str], env: dict[str, str]): |
| 143 | + command_path = shutil.which(command) |
| 144 | + if not command_path: |
| 145 | + raise click.UsageError(f"Command not found: {command}") |
| 146 | + |
| 147 | + try: |
| 148 | + # Only support stdio server type |
| 149 | + server_type = server_config.get("type", "stdio") |
| 150 | + if server_type != "stdio": |
| 151 | + raise click.UsageError(f"Only stdio MCP servers are supported. Found type: {server_type}") |
| 152 | + |
| 153 | + server_params = StdioServerParameters( |
| 154 | + command=command_path, |
| 155 | + args=args, |
| 156 | + env=env, |
| 157 | + ) |
| 158 | + |
| 159 | + async with stdio_client(server_params) as (read, write): |
| 160 | + async with ClientSession(read, write) as session: |
| 161 | + await session.initialize() |
| 162 | + tools = await session.list_tools() |
| 163 | + mcp_tools = [ |
| 164 | + { |
| 165 | + "name": tool.name, |
| 166 | + "description": tool.description, |
| 167 | + "inputSchema": tool.inputSchema, |
| 168 | + } |
| 169 | + for tool in tools.tools |
| 170 | + ] |
| 171 | + |
| 172 | + return { |
| 173 | + "tools": mcp_tools, |
| 174 | + } |
| 175 | + except Exception as e: |
| 176 | + raise click.UsageError("Could not connect to MCP server: " + str(e)) from e |
0 commit comments