MCPService
Swift
- Python Unavailable
- Ruby Unavailable
- Lua Unavailable
- TypeScript Unavailable
- Rust Unavailable
- Go Unavailable
- Java Unavailable
- .NET Unavailable
- C++ Unavailable
- Swift
- struct MCPService : struct MCPService
- Bases:
Serves MCP over a stream of request lines.
Every request runs in a task of its own, which is the whole point: a wait that blocks for thirty seconds must not stop
ping, a second wait, or the cancellation that would end it. Serving them one at a time makes the server's slowest tool its latency for everything.Discussed in Swift MCP API
Members
- init(handler:) ( handler : MCPRequestHandler )
- serve(_:write:) ( lines : AsyncStream<String> , write : @escaping (String) async -> Void ) ()
-
Reads requests from
linesand writes each answer throughwrite.Returns once
lineshas ended *and* every request it carried has been answered. The end of input means no more requests, not abandon the ones in hand: a client that sent a batch and closed its end is still owed the replies, and cancelling them would drop answers that were already computed. Cancelling this call cancels active requests and queued output. Thewriteclosure must return cooperatively when its task is cancelled.AsyncStreamconcretely, rather than any non-throwingAsyncSequence: naming the failure type is what makes a sequence non-throwing, and that spelling needs macOS 15 where this package supports 13. Package-only transports share the generic request loop below.