Skip to content

Commit

Permalink
initial server support (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsabran authored Dec 28, 2024
1 parent 4cbe50a commit e0c71aa
Show file tree
Hide file tree
Showing 90 changed files with 4,867 additions and 2,610 deletions.
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 @@ extension DataChannel {
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
let process = Process()
process.executableURL = URL(fileURLWithPath: try path(for: executable))
process.arguments = args
Expand Down
Loading

0 comments on commit e0c71aa

Please sign in to comment.