Skip to content

Remove nonstandard read and list capabilities for resources #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2025
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ let server = Server(
name: "MyServer",
version: "1.0.0",
capabilities: .init(
prompts: .init(),
resources: .init(
list: true,
read: true,
subscribe: true
)
),
tools: .init()
)
)

Expand Down
4 changes: 2 additions & 2 deletions Sources/MCP/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public actor Client {
// MARK: - Resources

public func readResource(uri: String) async throws -> [Resource.Content] {
_ = try checkCapability(\.resources?.read, "Resource reading")
_ = try checkCapability(\.resources, "Resources")
let request = ReadResource.request(.init(uri: uri))
let result = try await send(request)
return result.contents
Expand All @@ -322,7 +322,7 @@ public actor Client {
public func listResources(cursor: String? = nil) async throws -> (
resources: [Resource], nextCursor: String?
) {
_ = try checkCapability(\.resources?.list, "Resource listing")
_ = try checkCapability(\.resources, "Resources")
let request = ListResources.request(.init(cursor: cursor))
let result = try await send(request)
return (resources: result.resources, nextCursor: result.nextCursor)
Expand Down
8 changes: 0 additions & 8 deletions Sources/MCP/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,15 @@ public actor Server {
public struct Capabilities: Hashable, Codable, Sendable {
/// Resources capabilities
public struct Resources: Hashable, Codable, Sendable {
/// Whether the list of resources has changed
public var list: Bool?
/// Whether the resource can be read
public var read: Bool?
/// Whether the resource can be subscribed to
public var subscribe: Bool?
/// Whether the list of resources has changed
public var listChanged: Bool?

public init(
list: Bool? = nil,
read: Bool? = nil,
subscribe: Bool? = nil,
listChanged: Bool? = nil
) {
self.list = list
self.read = read
self.subscribe = subscribe
self.listChanged = listChanged
}
Expand Down
70 changes: 69 additions & 1 deletion Tests/MCPTests/RoundtripTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ struct RoundtripTests {
let server = Server(
name: "TestServer",
version: "1.0.0",
capabilities: .init(prompts: .init(), tools: .init())
capabilities: .init(
prompts: .init(),
resources: .init(),
tools: .init()
)
)
await server.withMethodHandler(ListTools.self) { _ in
return ListTools.Result(tools: [
Expand Down Expand Up @@ -61,6 +65,32 @@ struct RoundtripTests {
return CallTool.Result(content: [.text("\(a + b)")])
}

// Add resource handlers to server
await server.withMethodHandler(ListResources.self) { _ in
return ListResources.Result(resources: [
Resource(
name: "Example Text",
uri: "test://example.txt",
description: "A test resource",
mimeType: "text/plain"
),
Resource(
name: "Test Data",
uri: "test://data.json",
description: "JSON test data",
mimeType: "application/json"
),
])
}

await server.withMethodHandler(ReadResource.self) { request in
guard request.uri == "test://example.txt" else {
return ReadResource.Result(contents: [.text("Resource not found", uri: request.uri)]
)
}
return ReadResource.Result(contents: [.text("Hello, World!", uri: request.uri)])
}

let client = Client(name: "TestClient", version: "1.0")

try await server.start(transport: serverTransport)
Expand Down Expand Up @@ -104,15 +134,53 @@ struct RoundtripTests {
group.addTask {
try await Task.sleep(for: .seconds(1))
listToolsTask.cancel()
callToolTask.cancel()
throw CancellationError()
}
group.addTask {
try await listToolsTask.value
}
group.addTask {
try await callToolTask.value
}
try await group.next()
group.cancelAll()
}

// Test listing resources
let listResourcesTask = Task {
let result = try await client.listResources()
#expect(result.resources.count == 2)
#expect(result.resources[0].uri == "test://example.txt")
#expect(result.resources[0].name == "Example Text")
#expect(result.resources[1].uri == "test://data.json")
#expect(result.resources[1].name == "Test Data")
}

// Test reading a resource
let readResourceTask = Task {
let result = try await client.readResource(uri: "test://example.txt")
#expect(result.count == 1)
#expect(result[0] == .text("Hello, World!", uri: "test://example.txt"))
}

try await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await Task.sleep(for: .seconds(1))
listResourcesTask.cancel()
readResourceTask.cancel()
throw CancellationError()
}
group.addTask {
try await listResourcesTask.value
}
group.addTask {
try await readResourceTask.value
}
try await group.next()
group.cancelAll()
}

await server.stop()
await client.disconnect()
try? clientToServerRead.close()
Expand Down