Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions ExampleMCPServer/Sources/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Foundation
import JSONSchemaBuilder
import MCPServer

let transport = Transport.stdio()
func proxy(_ transport: Transport) -> Transport {
var sendToDataSequence: AsyncStream<Data>.Continuation?
let dataSequence = AsyncStream<Data>.init { continuation in
sendToDataSequence = continuation
}

Task {
for await data in transport.dataSequence {
mcpLogger.info("Reading data from transport: \(String(data: data, encoding: .utf8)!, privacy: .public)")
sendToDataSequence?.yield(data)
}
}

return Transport(
writeHandler: { data in
mcpLogger.info("Writing data to transport: \(String(data: data, encoding: .utf8)!, privacy: .public)")
try await transport.writeHandler(data)
},
dataSequence: dataSequence)
}

// MARK: - RepeatToolInput

@Schemable
struct RepeatToolInput {
let text: String
}

let server = try await MCPServer(
info: Implementation(name: "test-server", version: "1.0.0"),
capabilities: ServerCapabilityHandlers(tools: [
Tool(name: "repeat") { (input: RepeatToolInput) in
[.text(.init(text: input.text))]
},
]),
transport: proxy(transport))

try await server.waitForDisconnection()
4 changes: 4 additions & 0 deletions ExampleMCPServer/launch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/zsh

dir=$(dirname "$0")
(cd "$dir/.." && swift run ExampleMCPServer -q)
11 changes: 11 additions & 0 deletions ExampleMCPServer/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Inspect the server in the debugger:

```
nvm use 20.18.1

npx @modelcontextprotocol/inspector "$(pwd)/ExampleMCPServer/launch.sh"
```


# Observe console logs:
- in Console.app, filter by `com.app.mcp` as the subsystem.
1 change: 1 addition & 0 deletions MCPClient/Sources/DataChannel+StdioProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
return path.isEmpty ? executable : path
}

// TODO: look at how to use /bin/zsh, at least on MacOS, to avoid needing to specify PATH to locate the executable

Check warning on line 66 in MCPClient/Sources/DataChannel+StdioProcess.swift

View check run for this annotation

Codecov / codecov/patch

MCPClient/Sources/DataChannel+StdioProcess.swift#L66

Added line #L66 was not covered by tests
let process = Process()
process.executableURL = URL(fileURLWithPath: try path(for: executable))
process.arguments = args
Expand Down
Loading