Skip to content

Commit a7cedae

Browse files
committed
feat: add mcp command group with create-mcp-proxy functionality
1 parent e652074 commit a7cedae

File tree

1 file changed

+59
-0
lines changed
  • src/datapilot/core/mcp_utils

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import asyncio
2+
import logging
3+
import json
4+
import shutil
5+
6+
from mcp import ClientSession, StdioServerParameters
7+
from mcp.client.stdio import stdio_client
8+
import click
9+
10+
11+
logging.basicConfig(level=logging.INFO)
12+
13+
# New mcp group
14+
@click.group()
15+
def mcp():
16+
"""mcp specific commands."""
17+
18+
19+
@mcp.command("create-mcp-proxy")
20+
def create_mcp_proxy():
21+
content = click.edit()
22+
if content is None:
23+
click.echo("No input provided.")
24+
25+
output = asyncio.run(list_tools())
26+
click.echo(json.dumps(output, indent=2))
27+
28+
async def list_tools(command: str, args: list[str], env: dict[str, str]) -> str:
29+
command = shutil.which(command)
30+
31+
# Create server parameters for stdio connection
32+
server_params = StdioServerParameters(
33+
command=command, # Executable
34+
args=args, # Optional command line arguments
35+
env=None, # Optional environment variables
36+
)
37+
38+
async with stdio_client(server_params) as (read, write):
39+
async with ClientSession(
40+
read, write
41+
) as session:
42+
# Initialize the connection
43+
await session.initialize()
44+
45+
# List available tools
46+
tools = await session.list_tools()
47+
48+
# print as json
49+
tools_list = [
50+
{
51+
"name": tool.name,
52+
"description": tool.description,
53+
"inputSchema": tool.inputSchema,
54+
}
55+
for tool in tools.tools
56+
]
57+
58+
return tools_list
59+

0 commit comments

Comments
 (0)