Connect to local MCP servers over stdio

This commit is contained in:
Mark Gregory
2026-11-16 14:04:16 +00:00
parent caa0a8733b
commit 515f609f8d
6 changed files with 357 additions and 23 deletions
+43
View File
@@ -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()
}
}