Skip to content

Commit 17b03eb

Browse files
added example client
1 parent 6c316fe commit 17b03eb

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Spins up a test client to interact with MCP server in SSE-mode."""
2+
import os
3+
import asyncio
4+
import json
5+
import sys
6+
from mando import command, main
7+
from mcp import ClientSession
8+
from mcp.client.streamable_http import streamablehttp_client
9+
10+
API_KEY=os.environ.get('API_KEY')
11+
MCP_SERVER_URL = os.getenv("MCP_SERVER_URL", "http://localhost:8000").rstrip("/") + '/mcp/'
12+
13+
async def run(method_name: str, raw_args: str = None):
14+
"""Generalized runner for MCP client methods."""
15+
try:
16+
args = json.loads(raw_args) if raw_args else {}
17+
except json.JSONDecodeError:
18+
raise ValueError(f"Could not parse JSON args: {raw_args}")
19+
20+
headers = {"Authorization": f"Bearer {API_KEY}"}
21+
22+
# Proper unpacking: don't treat the 3rd item as part of the session
23+
async with streamablehttp_client(MCP_SERVER_URL, headers=headers) as (read_stream, write_stream, get_session_id):
24+
async with ClientSession(read_stream, write_stream) as session:
25+
await session.initialize()
26+
method = getattr(session, method_name)
27+
return await method(**args)
28+
29+
30+
@command
31+
def mcp(method_name, args=None):
32+
result = asyncio.run(run(method_name, args))
33+
print(json.dumps(result.model_dump(), indent=2))
34+
35+
if __name__ == "__main__" and not hasattr(sys, "ps1"):
36+
main()

0 commit comments

Comments
 (0)