Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit e0c71aa

Browse files
authored
initial server support (#6)
1 parent 4cbe50a commit e0c71aa

File tree

90 files changed

+4867
-2610
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+4867
-2610
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Foundation
2+
import JSONSchemaBuilder
3+
import MCPServer
4+
5+
let transport = Transport.stdio()
6+
func proxy(_ transport: Transport) -> Transport {
7+
var sendToDataSequence: AsyncStream<Data>.Continuation?
8+
let dataSequence = AsyncStream<Data>.init { continuation in
9+
sendToDataSequence = continuation
10+
}
11+
12+
Task {
13+
for await data in transport.dataSequence {
14+
mcpLogger.info("Reading data from transport: \(String(data: data, encoding: .utf8)!, privacy: .public)")
15+
sendToDataSequence?.yield(data)
16+
}
17+
}
18+
19+
return Transport(
20+
writeHandler: { data in
21+
mcpLogger.info("Writing data to transport: \(String(data: data, encoding: .utf8)!, privacy: .public)")
22+
try await transport.writeHandler(data)
23+
},
24+
dataSequence: dataSequence)
25+
}
26+
27+
// MARK: - RepeatToolInput
28+
29+
@Schemable
30+
struct RepeatToolInput {
31+
let text: String
32+
}
33+
34+
let server = try await MCPServer(
35+
info: Implementation(name: "test-server", version: "1.0.0"),
36+
capabilities: ServerCapabilityHandlers(tools: [
37+
Tool(name: "repeat") { (input: RepeatToolInput) in
38+
[.text(.init(text: input.text))]
39+
},
40+
]),
41+
transport: proxy(transport))
42+
43+
try await server.waitForDisconnection()

ExampleMCPServer/launch.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/zsh
2+
3+
dir=$(dirname "$0")
4+
(cd "$dir/.." && swift run ExampleMCPServer -q)

ExampleMCPServer/readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Inspect the server in the debugger:
2+
3+
```
4+
nvm use 20.18.1
5+
6+
npx @modelcontextprotocol/inspector "$(pwd)/ExampleMCPServer/launch.sh"
7+
```
8+
9+
10+
# Observe console logs:
11+
- in Console.app, filter by `com.app.mcp` as the subsystem.

MCPClient/Sources/DataChannel+StdioProcess.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ extension DataChannel {
6363
return path.isEmpty ? executable : path
6464
}
6565

66+
// TODO: look at how to use /bin/zsh, at least on MacOS, to avoid needing to specify PATH to locate the executable
6667
let process = Process()
6768
process.executableURL = URL(fileURLWithPath: try path(for: executable))
6869
process.arguments = args

0 commit comments

Comments
 (0)