Skip to content

Commit 8a50d89

Browse files
authored
docs: fix code example in README.md (#141)
1 parent 7c58239 commit 8a50d89

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

README.md

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Tools represent functions that can be called by the client:
9898

9999
```swift
100100
// List available tools
101-
let tools = try await client.listTools()
101+
let (tools, cursor) = try await client.listTools()
102102
print("Available tools: \(tools.map { $0.name }.joined(separator: ", "))")
103103

104104
// Call a tool with arguments
@@ -335,15 +335,15 @@ Improve performance by sending multiple requests in a single batch:
335335

336336
```swift
337337
// Array to hold tool call tasks
338-
var toolTasks: [Task<CallTool.Result, Error>] = []
338+
var toolTasks: [Task<CallTool.Result, Swift.Error>] = []
339339

340340
// Send a batch of requests
341341
try await client.withBatch { batch in
342342
// Add multiple tool calls to the batch
343343
for i in 0..<10 {
344344
toolTasks.append(
345345
try await batch.addRequest(
346-
CallTool.request(.init(name: "square", arguments: ["n": i]))
346+
CallTool.request(.init(name: "square", arguments: ["n": Value(i)]))
347347
)
348348
)
349349
}
@@ -428,7 +428,7 @@ Register tool handlers to respond to client tool calls:
428428

429429
```swift
430430
// Register a tool list handler
431-
server.withMethodHandler(ListTools.self) { _ in
431+
await server.withMethodHandler(ListTools.self) { _ in
432432
let tools = [
433433
Tool(
434434
name: "weather",
@@ -454,7 +454,7 @@ server.withMethodHandler(ListTools.self) { _ in
454454
}
455455

456456
// Register a tool call handler
457-
server.withMethodHandler(CallTool.self) { params in
457+
await server.withMethodHandler(CallTool.self) { params in
458458
switch params.name {
459459
case "weather":
460460
let location = params.arguments?["location"]?.stringValue ?? "Unknown"
@@ -485,24 +485,24 @@ Implement resource handlers for data access:
485485

486486
```swift
487487
// Register a resource list handler
488-
server.withMethodHandler(ListResources.self) { params in
488+
await server.withMethodHandler(ListResources.self) { params in
489489
let resources = [
490490
Resource(
491-
uri: "resource://knowledge-base/articles",
492491
name: "Knowledge Base Articles",
492+
uri: "resource://knowledge-base/articles",
493493
description: "Collection of support articles and documentation"
494494
),
495495
Resource(
496-
uri: "resource://system/status",
497496
name: "System Status",
497+
uri: "resource://system/status",
498498
description: "Current system operational status"
499499
)
500500
]
501501
return .init(resources: resources, nextCursor: nil)
502502
}
503503

504504
// Register a resource read handler
505-
server.withMethodHandler(ReadResource.self) { params in
505+
await server.withMethodHandler(ReadResource.self) { params in
506506
switch params.uri {
507507
case "resource://knowledge-base/articles":
508508
return .init(contents: [Resource.Content.text("# Knowledge Base\n\nThis is the content of the knowledge base...", uri: params.uri)])
@@ -528,7 +528,7 @@ server.withMethodHandler(ReadResource.self) { params in
528528
}
529529

530530
// Register a resource subscribe handler
531-
server.withMethodHandler(SubscribeToResource.self) { params in
531+
await server.withMethodHandler(ResourceSubscribe.self) { params in
532532
// Store subscription for later notifications.
533533
// Client identity for multi-client scenarios needs to be managed by the server application,
534534
// potentially using information from the initialize handshake if the server handles one client post-init.
@@ -544,7 +544,7 @@ Implement prompt handlers:
544544

545545
```swift
546546
// Register a prompt list handler
547-
server.withMethodHandler(ListPrompts.self) { params in
547+
await server.withMethodHandler(ListPrompts.self) { params in
548548
let prompts = [
549549
Prompt(
550550
name: "interview",
@@ -568,7 +568,7 @@ server.withMethodHandler(ListPrompts.self) { params in
568568
}
569569

570570
// Register a prompt get handler
571-
server.withMethodHandler(GetPrompt.self) { params in
571+
await server.withMethodHandler(GetPrompt.self) { params in
572572
switch params.name {
573573
case "interview":
574574
let position = params.arguments?["position"]?.stringValue ?? "Software Engineer"
@@ -618,8 +618,8 @@ do {
618618
.user("Analyze this data and suggest next steps")
619619
],
620620
systemPrompt: "You are a helpful data analyst",
621-
maxTokens: 150,
622-
temperature: 0.7
621+
temperature: 0.7,
622+
maxTokens: 150
623623
)
624624

625625
// Use the LLM completion in your server logic
@@ -649,8 +649,8 @@ try await server.start(transport: transport) { clientInfo, clientCapabilities in
649649
}
650650

651651
// You can also inspect client capabilities
652-
if clientCapabilities.tools == nil {
653-
print("Client does not support tools")
652+
if clientCapabilities.sampling == nil {
653+
print("Client does not support sampling")
654654
}
655655

656656
// Perform any server-side setup based on client info
@@ -720,19 +720,18 @@ let server = Server(
720720
prompts: .init(listChanged: true),
721721
resources: .init(subscribe: true, listChanged: true),
722722
tools: .init(listChanged: true)
723-
),
724-
logger: logger
723+
)
725724
)
726725

727726
// Add handlers directly to the server
728-
server.withMethodHandler(ListTools.self) { _ in
727+
await server.withMethodHandler(ListTools.self) { _ in
729728
// Your implementation
730729
return .init(tools: [
731730
Tool(name: "example", description: "An example tool")
732731
])
733732
}
734733

735-
server.withMethodHandler(CallTool.self) { params in
734+
await server.withMethodHandler(CallTool.self) { params in
736735
// Your implementation
737736
return .init(content: [.text("Tool result")], isError: false)
738737
}
@@ -791,13 +790,13 @@ import Foundation
791790
public actor MyCustomTransport: Transport {
792791
public nonisolated let logger: Logger
793792
private var isConnected = false
794-
private let messageStream: AsyncThrowingStream<Data, Error>
795-
private let messageContinuation: AsyncThrowingStream<Data, Error>.Continuation
793+
private let messageStream: AsyncThrowingStream<Data, any Swift.Error>
794+
private let messageContinuation: AsyncThrowingStream<Data, any Swift.Error>.Continuation
796795

797796
public init(logger: Logger? = nil) {
798797
self.logger = logger ?? Logger(label: "my.custom.transport")
799798

800-
var continuation: AsyncThrowingStream<Data, Error>.Continuation!
799+
var continuation: AsyncThrowingStream<Data, any Swift.Error>.Continuation!
801800
self.messageStream = AsyncThrowingStream { continuation = $0 }
802801
self.messageContinuation = continuation
803802
}
@@ -817,7 +816,7 @@ public actor MyCustomTransport: Transport {
817816
// Implement your message sending logic
818817
}
819818

820-
public func receive() -> AsyncThrowingStream<Data, Error> {
819+
public func receive() -> AsyncThrowingStream<Data, any Swift.Error> {
821820
return messageStream
822821
}
823822
}
@@ -863,7 +862,7 @@ LoggingSystem.bootstrap { label in
863862
let logger = Logger(label: "com.example.mcp")
864863

865864
// Pass to client/server
866-
let client = Client(name: "MyApp", version: "1.0.0", logger: logger)
865+
let client = Client(name: "MyApp", version: "1.0.0")
867866

868867
// Pass to transport
869868
let transport = StdioTransport(logger: logger)

0 commit comments

Comments
 (0)