Skip to content
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
19 changes: 17 additions & 2 deletions Sources/MCP/Base/Lifecycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
/// - Exchange and negotiate capabilities
/// - Share implementation details
///
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/lifecycle/#initialization
/// `initialize` exists only in the handshake revisions (``Version/handshake``). 2026-07-28 removes
/// it ([SEP-2575](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/2575)): each
/// request carries its protocol version and client capabilities in `_meta`, and `server/discover`
/// advertises what the server supports. A server that serves stateless requests answers a stateless
/// `initialize` with -32601 (method not found). A client in ``Client/ProtocolMode/automatic`` mode
/// sends `server/discover` first and falls back to `initialize` on the signals that mode lists.
///
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/lifecycle#initialization
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/changelog
public enum Initialize: Method {
public static let name: String = "initialize"

Expand Down Expand Up @@ -101,7 +109,14 @@ public enum Initialize: Method {
}

/// After successful initialization, the client MUST send an initialized notification to indicate it is ready to begin normal operations.
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/lifecycle/#initialization
///
/// Like ``Initialize``, `notifications/initialized` exists only in the handshake revisions;
/// 2026-07-28 removes it
/// ([SEP-2575](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/2575)). A client
/// sends it only after an `initialize` exchange.
///
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/lifecycle#initialization
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/changelog
public struct InitializedNotification: Notification {
public static let name: String = "notifications/initialized"
}
2 changes: 1 addition & 1 deletion Sources/MCP/Base/Transports/HTTPClientTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Logging

/// An implementation of the MCP Streamable HTTP transport protocol for clients.
///
/// This transport implements the [Streamable HTTP transport](https://spec.modelcontextprotocol.io/specification/2025-11-25/basic/transports#streamable-http)
/// This transport implements the [Streamable HTTP transport](https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#streamable-http)
/// of the Model Context Protocol. On stateless (2026-07-28) connections it
/// also sends the `Mcp-Method`, `Mcp-Name` and `Mcp-Param-*` request headers.
///
Expand Down
10 changes: 8 additions & 2 deletions Sources/MCP/Base/Utilities/Cancellation.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import Foundation

/// The Model Context Protocol supports cancellation of requests through
/// notification messages. Either side can send a cancellation notification
/// to indicate that a previously-issued request should be cancelled.
/// notification messages.
///
/// In the handshake revisions either side sends it to cancel a request it
/// sent. At 2026-07-28 a client sends it on every transport except Streamable
/// HTTP, where closing the request's response stream cancels the request, and
/// a server sends it only to tear down a `subscriptions/listen` stream the
/// client opened.
///
/// Cancellation is advisory: the receiver should make a best-effort attempt
/// to cancel the operation, but may not always be able to do so.
///
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/cancellation
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/basic/patterns/cancellation
public struct CancelledNotification: Notification {
public static let name: String = "notifications/cancelled"

Expand Down
9 changes: 8 additions & 1 deletion Sources/MCP/Base/Utilities/Ping.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/// 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
///
/// `ping` exists only in the revisions that use the `initialize` handshake. 2026-07-28 removes it
/// ([SEP-2575](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/2575)): a server that
/// serves stateless requests answers a stateless `ping` with -32601 (method not found), and
/// ``Client/ping()`` sends `server/discover` instead on a stateless connection.
///
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/ping
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/changelog
public enum Ping: Method {
public static let name: String = "ping"
}
6 changes: 5 additions & 1 deletion Sources/MCP/Base/Utilities/Progress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import Foundation
/// The sender (either client or server) that issued the original request may
/// include a progress token in the request's `_meta` field. If the receiver
/// supports progress reporting, it can send progress notifications
/// containing that token to indicate how the operation is proceeding.
/// containing that token to indicate how the operation is proceeding. At
/// 2026-07-28, which has no server-to-client requests, only the server sends
/// them, for requests the client issued.
///
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/progress
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/basic/patterns/progress
public struct ProgressNotification: Notification {
public static let name: String = "notifications/progress"

Expand Down Expand Up @@ -97,6 +100,7 @@ public enum ProgressToken: Hashable, Codable, Sendable {
/// which can contain a progress token for receiving progress notifications.
///
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/progress
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/basic/patterns/progress
public struct Metadata: Hashable, Codable, Sendable {
/// The field for `key`, or `nil` when it is absent.
public subscript(_ key: String) -> Value? {
Expand Down
11 changes: 7 additions & 4 deletions Sources/MCP/Base/Utilities/RequestContext.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import Foundation

/// A context object that wraps a pending request, providing both the request ID
/// and a Task handle for the asynchronous operation.
/// A request the client sent: its ID and the task that awaits its response.
///
/// This allows you to track and cancel in-flight requests by sending
/// a CancelledNotification to the receiver (client or server).
/// Cancel the request with `Client.cancelRequest(_:reason:)` or by cancelling
/// the task that awaits ``value``. How the cancellation reaches the server
/// depends on the revision and transport: at 2026-07-28 over Streamable HTTP
/// the client closes the request's response stream; otherwise it sends
/// `notifications/cancelled`.
///
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/cancellation
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/basic/patterns/cancellation#transport-specific-cancellation
public struct RequestContext<Output: Sendable & Decodable>: Sendable {
/// The unique identifier for this request.
public let requestID: ID
Expand Down
13 changes: 10 additions & 3 deletions Sources/MCP/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1113,8 +1113,8 @@ public actor Client {

/// Send a request and return a RequestContext that wraps both the request ID and the Task.
///
/// This allows you to track and cancel the request by sending a CancelledNotification
/// to the server using the requestID.
/// Cancel the request with ``cancelRequest(_:reason:)``, passing the
/// context's `requestID`, or by cancelling the task that awaits its `value`.
///
/// Example:
/// ```swift
Expand All @@ -1129,6 +1129,7 @@ public actor Client {
/// - Returns: A RequestContext containing the request ID and Task
/// - Throws: MCPError if the client is not connected
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/cancellation
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/basic/patterns/cancellation#transport-specific-cancellation
public func send<M: Method>(_ request: Request<M>) throws -> RequestContext<M.Result> {
let first = try dispatch(request)
return RequestContext(requestID: request.id, requestTask: withRoundTrips(request, first: first))
Expand Down Expand Up @@ -1631,7 +1632,12 @@ public actor Client {
listenAcknowledgements[id]?.yield(.success(notifications))
}

/// Cancel a request by sending a CancelledNotification to the server.
/// Cancels a request the client sent.
///
/// At 2026-07-28 over Streamable HTTP, this closes the request's response
/// stream, which is the cancellation signal there, and `reason` is not
/// sent. Otherwise it sends `notifications/cancelled` carrying `reason`.
/// A request not yet sent is never sent, and the server is told nothing.
///
/// According to the MCP specification, cancellation is advisory:
/// - The server SHOULD stop processing and free resources
Expand All @@ -1647,6 +1653,7 @@ public actor Client {
/// - reason: An optional human-readable reason for the cancellation
/// - Throws: MCPError if the notification cannot be sent
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/cancellation
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/basic/patterns/cancellation#transport-specific-cancellation
public func cancelRequest(_ requestID: ID, reason: String? = nil) async throws {
// A request in a multi round-trip exchange: stop its rounds, which
// cancels running input handlers, and cancel its current round.
Expand Down
2 changes: 1 addition & 1 deletion Sources/MCP/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,7 @@ public actor Server {
/// response is the teardown, so nothing is sent there; nor after a
/// stream the client cancelled.
///
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/basic/utilities/cancellation
/// - SeeAlso: https://modelcontextprotocol.io/specification/2026-07-28/basic/patterns/cancellation
private func sendListenTeardown(_ id: ID) async {
guard !isSessionless, let connection else { return }
let cancelled = CancelledNotification.message(.init(requestId: id, reason: "Subscription closed"))
Expand Down
2 changes: 1 addition & 1 deletion Sources/MCP/Server/Tools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ extension CallTool.Result: InputRequiringResult {
}

/// When the list of available tools changes, servers that declared the listChanged capability SHOULD send a notification:
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2025-06-18/server/tools/#list-changed-notification
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/server/tools#list-changed-notification
public struct ToolListChangedNotification: Notification {
public static let name: String = "notifications/tools/list_changed"
}
Loading