You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hello, @mattt , I am a swift beginner, I want to write a swift version of client that mimics the python example
Now that I have started a python server before swift client initialization, I wonder if I need to inject something into StdioTransport() before client initialization? Do the initialization directly as in the documentation?
Actually, an example of code that launches the process, creates the Pipe() for stdin and stdout and connect them correctly to the transport would be appreciated. The Example in the README is not detailed enough to get started.
It is surprisingly hard to get it working in Swift.
One thing which is unclear to me is whose input and output is it ? The client ? The server ? The above kotlin code seems to imply these are the server ones.
But the StdioClientTransport code send() function writes on the output (which should be the input of the server)
I’m confused.
More than just an example, some other language SDKs treat process as a first-class citizen for std io, allowing you to launch tools like npx, uv, etc easily
I’m using that approach for now:
extensionProcess{funcstdioTransport(logger:Logger?=nil)->StdioTransport{letinput=Pipe()letoutput=Pipe()self.standardInput=inputself.standardOutput=output
// XXX: maybe be std err too (option); some return some log "message"
returnStdioTransport(input:FileDescriptor(rawValue:output.fileHandleForReading.fileDescriptor),output:FileDescriptor(rawValue:input.fileHandleForWriting.fileDescriptor),logger:logger)}}
with
full path for npx, uv etc... (using /usr/bin/which <exeName>) if simple name without /)
correct PATH in process env (for instance npx must be able to launch node, so I add npx parent directory to PATH)
@phimage thank youf or having shared the portion of code. That pat I have and is similar to yours. What I'm struggling with is the rest: launching the process, creating the MCP client, connecting, initializing it.
I launch the process, create the MCP client, connect it (and it works). But then the call to client.initialize() blocks.
@Keoooo You can use Process to run another program like uvx or npx as a subprocess from a Swift program. If you're distributing on the App Store, you'll need to work within the App Sandbox, which has additional caveats.
@Keoooo You can use Process to run another program like uvx or npx as a subprocess from a Swift program. If you're distributing on the App Store, you'll need to work within the App Sandbox, which has additional caveats.
Given how common it is to use subprocesses (especially for Node/Python-based MCP servers), do you envision the Swift SDK eventually providing a helper or utility function to abstract this setup?
@Keoooo Perhaps. There's a balance to strike between convenience and expressiveness. There are a lot of things that one might want configure when launching a subprocess, which many / most users won't need to know about that are essential to other users. I suspect a mix of documentation and API affordances will be the best approach here.
@Keoooo Here is the code I use and that works for me (I want to migrate to the new SubProcess library from SwiftLang group)
packageinit(withtoolConfig:MCPConfiguration.ToolConfiguration,name:String,forserver:String,version:String="1.0.0",logger:Logger)asyncthrows{self.name=nameself.client=Client(name:name,version:version)self.logger=loggerlogger.trace("Launching process",metadata:["command":"\(toolConfig.command)","arguments":"\(toolConfig.args.joined(separator:""))",])
// Create pipes for the server input and output
letserverInputPipe=Pipe()letserverOutputPipe=Pipe()letserverInput:FileDescriptor=FileDescriptor(rawValue:serverInputPipe.fileHandleForWriting.fileDescriptor)letserverOutput:FileDescriptor=FileDescriptor(rawValue:serverOutputPipe.fileHandleForReading.fileDescriptor)self.process=Process()process.executableURL=URL(fileURLWithPath:toolConfig.command)process.arguments=toolConfig.argsprocess.standardInput=serverInputPipeprocess.standardOutput=serverOutputPipelettransport=StdioTransport(input:serverOutput,output:serverInput,logger:logger)tryprocess.run()logger.trace("Process launched")tryawaitclient.connect(transport:transport)logger.trace("Connected to MCP server")
// Initialize the connection
letresult=tryawaitclient.initialize()logger.trace("Connection initialized",metadata:["result":"\(result)"])
// collect the list of tools available in the MCP server
// var cursor: String? = nil
(self.tools,_)=tryawaitclient.listTools()logger.trace("Available tools",metadata:["toolsCount":"\(self.tools.count)"])}
As a new example/test for using the Client class and Process, I’ve created a quick and dirty command-line app to chat with the new FoundationModel using MCP server tools, based on Claude config files: https://github.com/phimage/FoundationModelCli
it states that “In the stdio transport” there should be a Launch subprocess step. I was wondering if you could kindly explain why the process would be executed outside of StdioTransport.
On rel/1.0.0 (#372), we've added a complete quick-start guide (docs/quick_start.md#a-stdio-client), a runnable Examples/hello-client target, and DocC tutorials (#362, #417) showing how to spawn a subprocess with Process and pass its pipes to StdioTransport(input:output:). StdioTransport intentionally takes file descriptors rather than launching a process itself so it stays cross-platform (including platforms without Process and sandboxed hosts where the app controls process lifecycle). Also, as of #360, if the spawned server exits early, client.connect(transport:) now throws MCPError.connectionClosed rather than hanging. Closing as answered.
Activity
MenJH commentedon Apr 7, 2025
In kotlin's example, StdioClientTransport() is set this way, and I was wondering how it should be set in swift?

sebsto commentedon Apr 16, 2025
Actually, an example of code that launches the process, creates the Pipe() for stdin and stdout and connect them correctly to the transport would be appreciated. The Example in the README is not detailed enough to get started.
It is surprisingly hard to get it working in Swift.
One thing which is unclear to me is whose input and output is it ? The client ? The server ? The above kotlin code seems to imply these are the server ones.
But the StdioClientTransport code send() function writes on the output (which should be the input of the server)
I’m confused.
sebsto commentedon Apr 20, 2025
I tried to spawn the process using the new
Subprocesspackage from Swift Lang.https://github.com/swiftlang/swift-subprocess
It blocks on
client.initailize(). Is this expected ? Should I launch this in a separate task ? @matttThis outputs
phimage commentedon Apr 20, 2025
More than just an example, some other language SDKs treat process as a first-class citizen for std io, allowing you to launch tools like npx, uv, etc easily
I’m using that approach for now:
with
/usr/bin/which <exeName>)if simple name without /)sebsto commentedon Apr 21, 2025
@phimage thank youf or having shared the portion of code. That pat I have and is similar to yours. What I'm struggling with is the rest: launching the process, creating the MCP client, connecting, initializing it.
I launch the process, create the MCP client, connect it (and it works). But then the call to
client.initialize()blocks.Do you have the full code snippet ?
phimage commentedon Apr 21, 2025
I share here https://github.com/phimage/mcp-cli-test a macOS CLI test app that launch a "npx" mcp server (npx installed with homebrew)
sebsto commentedon Apr 21, 2025
Thank you so much.
I made two mistakes.
inputandoutputmust be inverted on theStdioTransport(inputgoes tooutputand the opposite)Merci Beaucoup. I hope it will help others too.
Keoooo commentedon Jun 1, 2025
Hello, is there documentation anywhere that will help me understand how to run npm and Python MCP servers from within Swift applications?
mattt commentedon Jun 1, 2025
@Keoooo You can use
Processto run another program likeuvxornpxas a subprocess from a Swift program. If you're distributing on the App Store, you'll need to work within the App Sandbox, which has additional caveats.Keoooo commentedon Jun 1, 2025
Given how common it is to use subprocesses (especially for Node/Python-based MCP servers), do you envision the Swift SDK eventually providing a helper or utility function to abstract this setup?
mattt commentedon Jun 2, 2025
@Keoooo Perhaps. There's a balance to strike between convenience and expressiveness. There are a lot of things that one might want configure when launching a subprocess, which many / most users won't need to know about that are essential to other users. I suspect a mix of documentation and API affordances will be the best approach here.
sebsto commentedon Jun 5, 2025
@Keoooo Here is the code I use and that works for me (I want to migrate to the new SubProcess library from SwiftLang group)
phimage commentedon Jun 13, 2025
The Kotlin SDK includes some samples within the project:
https://github.com/modelcontextprotocol/kotlin-sdk/tree/main/samples
Maybe we could do something similar. (Just a suggestion—I personally prefer independent projects, as a Swift developer.)
As a new example/test for using the Client class and Process, I’ve created a quick and dirty command-line app to chat with the new FoundationModel using MCP server tools, based on Claude config files:
https://github.com/phimage/FoundationModelCli
ptliddle commentedon Jul 2, 2025
I'm a bit late to this thread but i wrote a step-by-step blog post on how to do this here. Building a MCP Client in Swift
sebsto commentedon Jul 2, 2025
Talking about MCP helper, I wrote this library that should help to write your servers and clients
https://github.com/sebsto/mcpserverkit
dcracker commentedon Oct 17, 2025
Hello, I hope you don’t mind me asking a possibly naive question.
According to this document:
https://modelcontextprotocol.io/specification/2025-03-26/basic/transports
it states that “In the stdio transport” there should be a Launch subprocess step. I was wondering if you could kindly explain why the process would be executed outside of StdioTransport.
Thank you very much for your time and help!
jeanmatthieu commentedon Dec 5, 2026
On
rel/1.0.0(#372), we've added a complete quick-start guide (docs/quick_start.md#a-stdio-client), a runnableExamples/hello-clienttarget, and DocC tutorials (#362, #417) showing how to spawn a subprocess withProcessand pass its pipes toStdioTransport(input:output:).StdioTransportintentionally takes file descriptors rather than launching a process itself so it stays cross-platform (including platforms withoutProcessand sandboxed hosts where the app controls process lifecycle). Also, as of #360, if the spawned server exits early,client.connect(transport:)now throwsMCPError.connectionClosedrather than hanging. Closing as answered.