Connect to local MCP servers over stdio
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
|
||||
initialized = False
|
||||
tool = {
|
||||
"name": "echo",
|
||||
"description": "Echo the supplied text",
|
||||
"inputSchema": {"type": "object"},
|
||||
}
|
||||
|
||||
for line in sys.stdin:
|
||||
request = json.loads(line)
|
||||
method = request["method"]
|
||||
params = request.get("params", {})
|
||||
if method == "initialize":
|
||||
assert request["params"]["protocolVersion"] == "2025-11-25"
|
||||
result = {"protocolVersion": "2025-06-18", "capabilities": {"tools": {}}}
|
||||
elif method == "notifications/initialized":
|
||||
initialized = True
|
||||
continue
|
||||
elif method == "tools/list":
|
||||
assert initialized
|
||||
if params.get("cursor") == "next":
|
||||
result = {"tools": [tool]}
|
||||
else:
|
||||
result = {"tools": [], "nextCursor": "next"}
|
||||
elif method == "tools/call":
|
||||
assert initialized and params["name"] == "echo"
|
||||
if params["arguments"].get("wait"):
|
||||
time.sleep(20)
|
||||
result = {"content": [{"type": "text", "text": params["arguments"].get("text", "")}]}
|
||||
else:
|
||||
raise ValueError(method)
|
||||
print(json.dumps({"jsonrpc": "2.0", "method": "notifications/message"}), flush=True)
|
||||
print(json.dumps({"jsonrpc": "2.0", "id": request["id"], "result": result}), flush=True)
|
||||
@@ -387,3 +387,46 @@ final class CompatibleEndpointTests: XCTestCase {
|
||||
.thinking("Thinking"), .answer("Fixture response")])
|
||||
}
|
||||
}
|
||||
|
||||
final class StdioMCPTests: XCTestCase {
|
||||
private func client() throws -> StdioMCPClient {
|
||||
let env = ProcessInfo.processInfo.environment
|
||||
guard let python = env["AISTUDIO_TEST_STDIO_PYTHON"],
|
||||
let fixture = env["AISTUDIO_TEST_STDIO_FIXTURE"] else {
|
||||
throw XCTSkip("Run Scripts/test.sh to configure the stdio fixture.")
|
||||
}
|
||||
return try StdioMCPClient(path: python, arguments: ["-u", fixture])
|
||||
}
|
||||
|
||||
func testLocalDiscoveryInvocationAndDisconnect() async throws {
|
||||
let local = try client()
|
||||
let tools = try await local.listTools()
|
||||
XCTAssertEqual(tools.map(\.name), ["echo"])
|
||||
let response = try await local.callTool("echo", argumentsJSON: Data(#"{"text":"line one\nline two"}"#.utf8))
|
||||
XCTAssertTrue(String(decoding: response, as: UTF8.self).contains("line two"))
|
||||
await local.disconnect()
|
||||
do {
|
||||
_ = try await local.callTool("echo", argumentsJSON: Data("{}".utf8))
|
||||
XCTFail("Expected a disconnected client error.")
|
||||
} catch let error as StudioError {
|
||||
XCTAssertTrue(error.message.contains("Discover"))
|
||||
}
|
||||
}
|
||||
|
||||
func testCancellationStopsWaitingForTheLocalServer() async throws {
|
||||
let local = try client()
|
||||
_ = try await local.listTools()
|
||||
let request = Task {
|
||||
try await local.callTool("echo", argumentsJSON: Data(#"{"wait":true}"#.utf8))
|
||||
}
|
||||
try await Task.sleep(for: .milliseconds(200))
|
||||
request.cancel()
|
||||
do {
|
||||
_ = try await request.value
|
||||
XCTFail("Expected cancellation.")
|
||||
} catch is CancellationError {}
|
||||
let tools = try await local.listTools()
|
||||
XCTAssertEqual(tools.map(\.name), ["echo"])
|
||||
await local.disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user