Skip to content
Closed
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: 6 additions & 0 deletions Sources/MCP/Base/Messages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public struct Request<M: Method>: Hashable, Identifiable, Codable, Sendable {
// If params field is missing, use Empty
params = Empty() as! M.Parameters
}
} else if M.Parameters.self == Value.self {
// allow missing parameter for Value parameters. This is important for AnyRequest to be able to
// decode a message that does have an `id` but does not have a `params` object. Otherwise the
// incoming message would fail to decode and would instead decode as an AnyNotification, despite
// the `id` parameter.
params = try container.decodeIfPresent(M.Parameters.self, forKey: .params) ?? (Value.null as! M.Parameters)
} else {
params = try container.decode(M.Parameters.self, forKey: .params)
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/MCP/Base/Utilities/Ping.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// The Model Context Protocol includes an optional ping mechanism that allows either party to verify that their counterpart is still responsive and the connection is alive.
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/utilities/ping
public enum Ping: Method {
public typealias Parameters = Empty
public typealias Result = Value
public static let name: String = "ping"
}
7 changes: 5 additions & 2 deletions Sources/MCP/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,11 @@ public actor Server {
return result
}

// Ping
withMethodHandler(Ping.self) { _ in return Empty() }
// Ping. Specification shows empty dictionary as the response:
// https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/utilities/ping/
withMethodHandler(Ping.self) { params in
return .object([:])
}
Comment on lines -404 to +408
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty is already encoded as an empty JSON object ({}), so we should be good here.

}

private func setInitialState(
Expand Down
6 changes: 5 additions & 1 deletion Sources/MCP/Server/Tools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ public struct Tool: Hashable, Codable, Sendable {
public enum ListTools: Method {
public static let name = "tools/list"

public struct Parameters: Hashable, Codable, Sendable {
// Specification allows optional cursor parameter:
// https://spec.modelcontextprotocol.io/specification/2024-11-05/server/tools/
public typealias Parameters = Cursor?

public struct Cursor: Hashable, Codable, Sendable {
public let cursor: String?

public init(cursor: String? = nil) {
Expand Down
Loading