V0.11.x (#199)
* Support 2025-06-18 Spec (#167) * Support OpenAI _meta for Tool * Update init * Add title and outputSchema * Concurreny issue * General fields * Add Generate Fields to Server elements * 4 tab spacing * Update Version * Elicitation * Format * More tests and inits * Elicitation in README * Fix orphaned test * Add title to more structs * Public init * Fix meta validation and make tests more generic * Format and make Result properties public again * Revert more visibility changes * feat: 2025-11-25 phase 1, includes Version, Icon, Progress (#181) * chore: fixed unit tests * feat: added icon and progess base structs * feat: added progress notification and meta to callTool * feat: added tests * feat: simplified RequestMeta encoding/decoding * tests: added unit test to track progress * chore: rebased v0.11.x * feat: implemented cancellation * feat: added overloaded callTool method * fix: encoding Response _meta fix * chore: added old version to the versions list * feat: 2025-11-25 phase 2; added Logging and Completions (#196) * feat: added Logging and Completions * feat: updated EmbeddedResource for the latest spec * chore: changed encoding of meta in Prompts to encodeIfPresent * feat: compliance tests + http server transport (#197) * feat: http server transport * feat: added http server transport * feat: removed unused files * feat: added conformance tests * feat: improved conformace testing * chore: removed redundant files * feat/2025-11-25: sampling, elicitation, roots (#198) * feat: Roots implementation * feat: added unit testing * feat: improved unit tests and fixed NetworkTransport crash * feat: implemented sampling and elicitation * feat: improved sampling tests * feat: improved elicitation tests * feat: improved confirmance tests * feat: improved Prompts unit tests * feat: removed test code * feat: updated conformance tests * chore: removed redundant commas * chore: removed 6.0.3 swift version from ci * chore: improved unit tests a little bit * chore: update README.md * chore: add server progress tracking example to README.md * chore: updated README.md * chore: updated transports table formatting * chore: updated MockNetworkConnection --------- Co-authored-by: Austin Evans <austinjevans@me.com> Co-authored-by: yehorsobko <yehorsobko@macpaw.com>
This commit is contained in:
parent
6fa1a97b41
commit
6112a3995a
3 changed files with 11854 additions and 828 deletions
29
Sources/MCP/Base/Utilities/Cancellation.swift
Normal file
29
Sources/MCP/Base/Utilities/Cancellation.swift
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
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.
|
||||
///
|
||||
/// 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
|
||||
public struct CancelledNotification: Notification {
|
||||
public static let name: String = "notifications/cancelled"
|
||||
|
||||
public struct Parameters: Hashable, Codable, Sendable {
|
||||
/// The ID of the request to cancel.
|
||||
///
|
||||
/// This MUST correspond to the ID of a request previously issued
|
||||
/// in the same direction.
|
||||
public let requestId: ID
|
||||
|
||||
/// An optional human-readable reason for the cancellation.
|
||||
public let reason: String?
|
||||
|
||||
public init(requestId: ID, reason: String? = nil) {
|
||||
self.requestId = requestId
|
||||
self.reason = reason
|
||||
}
|
||||
}
|
||||
}
|
||||
153
Sources/MCP/Base/Utilities/Progress.swift
Normal file
153
Sources/MCP/Base/Utilities/Progress.swift
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
import Foundation
|
||||
|
||||
/// Progress notifications are used to report progress on long-running operations.
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/progress
|
||||
public struct ProgressNotification: Notification {
|
||||
public static let name: String = "notifications/progress"
|
||||
|
||||
public struct Parameters: Hashable, Codable, Sendable {
|
||||
/// The progress token from the original request.
|
||||
///
|
||||
/// This is used to associate the progress notification with its
|
||||
/// originating request.
|
||||
public let progressToken: ProgressToken
|
||||
|
||||
/// The current progress value.
|
||||
///
|
||||
/// This should increase monotonically as the operation proceeds.
|
||||
/// It represents the amount of work completed so far.
|
||||
public let progress: Double
|
||||
|
||||
/// The total expected progress value, if known.
|
||||
///
|
||||
/// When provided, `progress / total` can be used to calculate
|
||||
/// a percentage completion.
|
||||
public let total: Double?
|
||||
|
||||
/// An optional human-readable message describing the current progress.
|
||||
public let message: String?
|
||||
|
||||
public init(
|
||||
progressToken: ProgressToken,
|
||||
progress: Double,
|
||||
total: Double? = nil,
|
||||
message: String? = nil
|
||||
) {
|
||||
self.progressToken = progressToken
|
||||
self.progress = progress
|
||||
self.total = total
|
||||
self.message = message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A token used to associate progress notifications with their originating request.
|
||||
///
|
||||
/// Progress tokens can be either strings or integers.
|
||||
/// Progress tokens MUST be unique across all active requests.
|
||||
public enum ProgressToken: Hashable, Codable, Sendable {
|
||||
case string(String)
|
||||
case integer(Int)
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
if let stringValue = try? container.decode(String.self) {
|
||||
self = .string(stringValue)
|
||||
} else if let intValue = try? container.decode(Int.self) {
|
||||
self = .integer(intValue)
|
||||
} else {
|
||||
throw DecodingError.dataCorruptedError(
|
||||
in: container,
|
||||
debugDescription: "Progress token must be a string or integer"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .string(let value):
|
||||
try container.encode(value)
|
||||
case .integer(let value):
|
||||
try container.encode(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a unique progress token using UUID.
|
||||
public static func unique() -> ProgressToken {
|
||||
.string(UUID().uuidString)
|
||||
}
|
||||
}
|
||||
|
||||
/// Metadata that can be included in request parameters.
|
||||
///
|
||||
/// This structure represents the `_meta` field in MCP request parameters,
|
||||
/// which can contain a progress token for receiving progress notifications.
|
||||
///
|
||||
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/progress
|
||||
public struct Metadata: Hashable, Codable, Sendable {
|
||||
public subscript(_ key: String) -> Value? {
|
||||
fields[key]
|
||||
}
|
||||
|
||||
/// The underlying fields dictionary.
|
||||
public var fields: [String: Value]
|
||||
|
||||
/// The progress token for receiving progress notifications.
|
||||
///
|
||||
/// If specified, the caller is requesting out-of-band progress notifications
|
||||
/// for this request. The value of this parameter is an opaque token that will
|
||||
/// be attached to any subsequent notifications. The receiver is not obligated
|
||||
/// to provide these notifications.
|
||||
public var progressToken: ProgressToken? {
|
||||
get {
|
||||
guard let value = fields["progressToken"] else { return nil }
|
||||
if let stringValue = value.stringValue {
|
||||
return .string(stringValue)
|
||||
} else if let intValue = value.intValue {
|
||||
return .integer(intValue)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
set {
|
||||
if let token = newValue {
|
||||
switch token {
|
||||
case .string(let s):
|
||||
fields["progressToken"] = .string(s)
|
||||
case .integer(let i):
|
||||
fields["progressToken"] = .int(i)
|
||||
}
|
||||
} else {
|
||||
fields.removeValue(forKey: "progressToken")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates request metadata.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - progressToken: Optional progress token for receiving progress notifications.
|
||||
/// If specified, the caller is requesting out-of-band progress notifications for this request.
|
||||
/// - additionalFields: Optional dictionary of additional metadata fields.
|
||||
/// These fields will be included in the `_meta` object alongside the progress token.
|
||||
public init(progressToken: ProgressToken? = nil, additionalFields: [String: Value] = [:]) {
|
||||
self.fields = additionalFields
|
||||
self.progressToken = progressToken
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
self.fields = try container.decode([String: Value].self)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(fields)
|
||||
}
|
||||
}
|
||||
34
Sources/MCP/Base/Utilities/RequestContext.swift
Normal file
34
Sources/MCP/Base/Utilities/RequestContext.swift
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import Foundation
|
||||
|
||||
/// A context object that wraps a pending request, providing both the request ID
|
||||
/// and a Task handle for the asynchronous operation.
|
||||
///
|
||||
/// This allows you to track and cancel in-flight requests by sending
|
||||
/// a CancelledNotification to the receiver (client or server).
|
||||
///
|
||||
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/cancellation
|
||||
public struct RequestContext<Output: Sendable & Decodable>: Sendable {
|
||||
/// The unique identifier for this request.
|
||||
public let requestID: ID
|
||||
|
||||
/// The Task representing the asynchronous work for this request.
|
||||
private let requestTask: Task<Output, Error>
|
||||
|
||||
/// Convenience property to await the result.
|
||||
///
|
||||
/// Example:
|
||||
/// ```swift
|
||||
/// let context = try await client.send(request)
|
||||
/// let result = try await context.value
|
||||
/// ```
|
||||
public var value: Output {
|
||||
get async throws {
|
||||
try await requestTask.value
|
||||
}
|
||||
}
|
||||
|
||||
public init(requestID: ID, requestTask: Task<Output, Error>) {
|
||||
self.requestID = requestID
|
||||
self.requestTask = requestTask
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue