-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_zcp_websocket.py
More file actions
87 lines (73 loc) · 2.42 KB
/
Copy pathtest_zcp_websocket.py
File metadata and controls
87 lines (73 loc) · 2.42 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
import asyncio
import json
from zcp import BearerAuthConfig, FastZCP, ZCPServerConfig, create_asgi_app
def build_ws_app():
app = FastZCP("WebSocket Test")
@app.tool(
name="weather.lookup",
description="Lookup weather.",
input_schema={
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
"additionalProperties": False,
},
output_mode="scalar",
inline_ok=True,
)
def weather_lookup(city: str):
return {"city": city, "temperature": 24}
return create_asgi_app(
app,
config=ZCPServerConfig(
service_name="ws-test",
auth=BearerAuthConfig(token="secret"),
),
)
async def invoke_websocket(app, messages):
sent = []
queue = asyncio.Queue()
for item in messages:
await queue.put(item)
await queue.put({"type": "websocket.disconnect"})
async def receive():
return await queue.get()
async def send(message):
sent.append(message)
scope = {
"type": "websocket",
"path": "/ws",
"headers": [(b"authorization", b"Bearer secret")],
"subprotocols": ["mcp"],
"client": ("127.0.0.1", 9999),
}
await app(scope, receive, send)
return sent
def test_websocket_mcp_surface() -> None:
app = build_ws_app()
async def run():
return await invoke_websocket(
app,
[
{
"type": "websocket.receive",
"text": json.dumps({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}),
},
{
"type": "websocket.receive",
"text": json.dumps(
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {"name": "weather.lookup", "arguments": {"city": "Hangzhou"}},
}
),
},
],
)
sent = asyncio.run(run())
assert sent[0]["type"] == "websocket.accept"
payloads = [json.loads(item["text"]) for item in sent if item["type"] == "websocket.send"]
assert payloads[0]["result"]["protocolVersion"] == "2025-11-25"
assert payloads[1]["result"]["structuredContent"]["city"] == "Hangzhou"