forked from modelcontextprotocol/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mcp_tool.py
83 lines (56 loc) · 1.81 KB
/
test_mcp_tool.py
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
import pytest
import os
import sys
from .mcp_stdio_client import MCPClient
from mcp import StdioServerParameters
# locate the exmaple MCP server co-located in this directory
mcp_server_dir = os.path.dirname(os.path.abspath(__file__))
mcp_server_file = os.path.join(mcp_server_dir, "example_mcp_server.py")
# mcpServers config in same syntax used by reference MCP
servers_config = {
"mcpServers": {
"testMcpServer": {
"command": "mcp", # be sure to . .venv/bin/activate so that mcp command is found
"args": [
"run",
mcp_server_file
]
}
}
}
# @pytest.mark.asyncio
@pytest.mark.anyio
async def test_mcp():
servers = servers_config.get("mcpServers")
server0 = "testMcpServer"
config0 = servers[server0]
client = MCPClient(
server0,
StdioServerParameters.model_validate(config0)
)
await client.initialize()
tools = await client.get_available_tools()
print(f"TOOLS:{tools}")
mcp_tool = tools[0]
res = await client.call_tool("simple_tool", {"x":5, "y":7})
print(f"RES:{res}")
# clients must be destroyed in reverse order
await client.cleanup()
# @pytest.mark.asyncio
@pytest.mark.anyio
async def test_mcp_with_logging():
servers = servers_config.get("mcpServers")
server0 = "testMcpServer"
config0 = servers[server0]
client = MCPClient(
server0,
StdioServerParameters.model_validate(config0)
)
await client.initialize()
tools = await client.get_available_tools()
print(f"TOOLS:{tools}")
mcp_tool = tools[0]
res = await client.call_tool("simple_tool_with_logging", {"x":5, "y":7})
print(f"RES:{res}")
# clients must be destroyed in reverse order
await client.cleanup()