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
1 changed files with 11854 additions and 828 deletions
|
|
@ -6,10 +6,12 @@ import Foundation
|
|||
/// such as files, database schemas, or application-specific information.
|
||||
/// Each resource is uniquely identified by a URI.
|
||||
///
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/server/resources/
|
||||
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/server/resources/
|
||||
public struct Resource: Hashable, Codable, Sendable {
|
||||
/// The resource name
|
||||
public var name: String
|
||||
/// A human-readable resource title
|
||||
public var title: String?
|
||||
/// The resource URI
|
||||
public var uri: String
|
||||
/// The resource description
|
||||
|
|
@ -18,19 +20,64 @@ public struct Resource: Hashable, Codable, Sendable {
|
|||
public var mimeType: String?
|
||||
/// The resource metadata
|
||||
public var metadata: [String: String]?
|
||||
/// Optional set of sized icons that the client can display in a user interface
|
||||
public var icons: [Icon]?
|
||||
/// Metadata fields for the resource (see spec for _meta usage)
|
||||
public var _meta: Metadata?
|
||||
|
||||
public init(
|
||||
name: String,
|
||||
uri: String,
|
||||
title: String? = nil,
|
||||
description: String? = nil,
|
||||
mimeType: String? = nil,
|
||||
metadata: [String: String]? = nil
|
||||
metadata: [String: String]? = nil,
|
||||
icons: [Icon]? = nil,
|
||||
_meta: Metadata? = nil
|
||||
) {
|
||||
self.name = name
|
||||
self.title = title
|
||||
self.uri = uri
|
||||
self.description = description
|
||||
self.mimeType = mimeType
|
||||
self.metadata = metadata
|
||||
self.icons = icons
|
||||
self._meta = _meta
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case name
|
||||
case uri
|
||||
case title
|
||||
case description
|
||||
case mimeType
|
||||
case metadata
|
||||
case icons
|
||||
case _meta
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
name = try container.decode(String.self, forKey: .name)
|
||||
uri = try container.decode(String.self, forKey: .uri)
|
||||
title = try container.decodeIfPresent(String.self, forKey: .title)
|
||||
description = try container.decodeIfPresent(String.self, forKey: .description)
|
||||
mimeType = try container.decodeIfPresent(String.self, forKey: .mimeType)
|
||||
metadata = try container.decodeIfPresent([String: String].self, forKey: .metadata)
|
||||
icons = try container.decodeIfPresent([Icon].self, forKey: .icons)
|
||||
_meta = try container.decodeIfPresent(Metadata.self, forKey: ._meta)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encode(uri, forKey: .uri)
|
||||
try container.encodeIfPresent(title, forKey: .title)
|
||||
try container.encodeIfPresent(description, forKey: .description)
|
||||
try container.encodeIfPresent(mimeType, forKey: .mimeType)
|
||||
try container.encodeIfPresent(metadata, forKey: .metadata)
|
||||
try container.encodeIfPresent(icons, forKey: .icons)
|
||||
try container.encodeIfPresent(_meta, forKey: ._meta)
|
||||
}
|
||||
|
||||
/// Content of a resource.
|
||||
|
|
@ -43,27 +90,82 @@ public struct Resource: Hashable, Codable, Sendable {
|
|||
public let text: String?
|
||||
/// The resource binary content
|
||||
public let blob: String?
|
||||
/// Metadata fields (see spec for _meta usage)
|
||||
public var _meta: Metadata?
|
||||
|
||||
public static func text(_ content: String, uri: String, mimeType: String? = nil) -> Self {
|
||||
.init(uri: uri, mimeType: mimeType, text: content)
|
||||
public static func text(
|
||||
_ content: String,
|
||||
uri: String,
|
||||
mimeType: String? = nil,
|
||||
_meta: Metadata? = nil
|
||||
) -> Self {
|
||||
.init(uri: uri, mimeType: mimeType, text: content, _meta: _meta)
|
||||
}
|
||||
|
||||
public static func binary(_ data: Data, uri: String, mimeType: String? = nil) -> Self {
|
||||
.init(uri: uri, mimeType: mimeType, blob: data.base64EncodedString())
|
||||
public static func binary(
|
||||
_ data: Data,
|
||||
uri: String,
|
||||
mimeType: String? = nil,
|
||||
_meta: Metadata? = nil
|
||||
) -> Self {
|
||||
.init(
|
||||
uri: uri,
|
||||
mimeType: mimeType,
|
||||
blob: data.base64EncodedString(),
|
||||
_meta: _meta
|
||||
)
|
||||
}
|
||||
|
||||
private init(uri: String, mimeType: String? = nil, text: String? = nil) {
|
||||
private init(
|
||||
uri: String,
|
||||
mimeType: String? = nil,
|
||||
text: String? = nil,
|
||||
_meta: Metadata? = nil
|
||||
) {
|
||||
self.uri = uri
|
||||
self.mimeType = mimeType
|
||||
self.text = text
|
||||
self.blob = nil
|
||||
self._meta = _meta
|
||||
}
|
||||
|
||||
private init(uri: String, mimeType: String? = nil, blob: String) {
|
||||
private init(
|
||||
uri: String,
|
||||
mimeType: String? = nil,
|
||||
blob: String,
|
||||
_meta: Metadata? = nil
|
||||
) {
|
||||
self.uri = uri
|
||||
self.mimeType = mimeType
|
||||
self.text = nil
|
||||
self.blob = blob
|
||||
self._meta = _meta
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case uri
|
||||
case mimeType
|
||||
case text
|
||||
case blob
|
||||
case _meta
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
uri = try container.decode(String.self, forKey: .uri)
|
||||
mimeType = try container.decodeIfPresent(String.self, forKey: .mimeType)
|
||||
text = try container.decodeIfPresent(String.self, forKey: .text)
|
||||
blob = try container.decodeIfPresent(String.self, forKey: .blob)
|
||||
_meta = try container.decodeIfPresent(Metadata.self, forKey: ._meta)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(uri, forKey: .uri)
|
||||
try container.encodeIfPresent(mimeType, forKey: .mimeType)
|
||||
try container.encodeIfPresent(text, forKey: .text)
|
||||
try container.encodeIfPresent(blob, forKey: .blob)
|
||||
try container.encodeIfPresent(_meta, forKey: ._meta)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -73,21 +175,57 @@ public struct Resource: Hashable, Codable, Sendable {
|
|||
public var uriTemplate: String
|
||||
/// The template name
|
||||
public var name: String
|
||||
/// A human-readable template title
|
||||
public var title: String?
|
||||
/// The template description
|
||||
public var description: String?
|
||||
/// The resource MIME type
|
||||
public var mimeType: String?
|
||||
/// Optional set of sized icons that the client can display in a user interface
|
||||
public var icons: [Icon]?
|
||||
|
||||
public init(
|
||||
uriTemplate: String,
|
||||
name: String,
|
||||
title: String? = nil,
|
||||
description: String? = nil,
|
||||
mimeType: String? = nil
|
||||
mimeType: String? = nil,
|
||||
icons: [Icon]? = nil
|
||||
) {
|
||||
self.uriTemplate = uriTemplate
|
||||
self.name = name
|
||||
self.title = title
|
||||
self.description = description
|
||||
self.mimeType = mimeType
|
||||
self.icons = icons
|
||||
}
|
||||
}
|
||||
|
||||
// A resource annotation.
|
||||
public struct Annotations: Hashable, Codable, Sendable {
|
||||
/// The intended audience for this resource.
|
||||
public enum Audience: String, Hashable, Codable, Sendable {
|
||||
/// Content intended for end users.
|
||||
case user = "user"
|
||||
/// Content intended for AI assistants.
|
||||
case assistant = "assistant"
|
||||
}
|
||||
|
||||
/// An array indicating the intended audience(s) for this resource. For example, `[.user, .assistant]` indicates content useful for both.
|
||||
public let audience: [Audience]?
|
||||
/// A number from 0.0 to 1.0 indicating the importance of this resource. A value of 1 means "most important" (effectively required), while 0 means "least important".
|
||||
public let priority: Double?
|
||||
/// An ISO 8601 formatted timestamp indicating when the resource was last modified (e.g., "2025-01-12T15:00:58Z").
|
||||
public let lastModified: String?
|
||||
|
||||
public init(
|
||||
audience: [Audience]? = nil,
|
||||
priority: Double? = nil,
|
||||
lastModified: String? = nil
|
||||
) {
|
||||
self.audience = audience
|
||||
self.priority = priority
|
||||
self.lastModified = lastModified
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -95,7 +233,7 @@ public struct Resource: Hashable, Codable, Sendable {
|
|||
// MARK: -
|
||||
|
||||
/// To discover available resources, clients send a `resources/list` request.
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/server/resources/#listing-resources
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2025-06-18/server/resources/#listing-resources
|
||||
public enum ListResources: Method {
|
||||
public static let name: String = "resources/list"
|
||||
|
||||
|
|
@ -105,7 +243,7 @@ public enum ListResources: Method {
|
|||
public init() {
|
||||
self.cursor = nil
|
||||
}
|
||||
|
||||
|
||||
public init(cursor: String) {
|
||||
self.cursor = cursor
|
||||
}
|
||||
|
|
@ -114,16 +252,40 @@ public enum ListResources: Method {
|
|||
public struct Result: Hashable, Codable, Sendable {
|
||||
public let resources: [Resource]
|
||||
public let nextCursor: String?
|
||||
public var _meta: Metadata?
|
||||
|
||||
public init(resources: [Resource], nextCursor: String? = nil) {
|
||||
public init(
|
||||
resources: [Resource],
|
||||
nextCursor: String? = nil,
|
||||
_meta: Metadata? = nil
|
||||
) {
|
||||
self.resources = resources
|
||||
self.nextCursor = nextCursor
|
||||
self._meta = _meta
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey, CaseIterable {
|
||||
case resources, nextCursor, _meta
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(resources, forKey: .resources)
|
||||
try container.encodeIfPresent(nextCursor, forKey: .nextCursor)
|
||||
try container.encodeIfPresent(_meta, forKey: ._meta)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
resources = try container.decode([Resource].self, forKey: .resources)
|
||||
nextCursor = try container.decodeIfPresent(String.self, forKey: .nextCursor)
|
||||
_meta = try container.decodeIfPresent(Metadata.self, forKey: ._meta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// To retrieve resource contents, clients send a `resources/read` request:
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/server/resources/#reading-resources
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2025-06-18/server/resources/#reading-resources
|
||||
public enum ReadResource: Method {
|
||||
public static let name: String = "resources/read"
|
||||
|
||||
|
|
@ -137,15 +299,37 @@ public enum ReadResource: Method {
|
|||
|
||||
public struct Result: Hashable, Codable, Sendable {
|
||||
public let contents: [Resource.Content]
|
||||
/// Optional metadata about this result
|
||||
public var _meta: Metadata?
|
||||
|
||||
public init(contents: [Resource.Content]) {
|
||||
public init(
|
||||
contents: [Resource.Content],
|
||||
_meta: Metadata? = nil
|
||||
) {
|
||||
self.contents = contents
|
||||
self._meta = _meta
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey, CaseIterable {
|
||||
case contents, _meta
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(contents, forKey: .contents)
|
||||
try container.encodeIfPresent(_meta, forKey: ._meta)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
contents = try container.decode([Resource.Content].self, forKey: .contents)
|
||||
_meta = try container.decodeIfPresent(Metadata.self, forKey: ._meta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// To discover available resource templates, clients send a `resources/templates/list` request.
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/server/resources/#resource-templates
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2025-06-18/server/resources/#resource-templates
|
||||
public enum ListResourceTemplates: Method {
|
||||
public static let name: String = "resources/templates/list"
|
||||
|
||||
|
|
@ -155,7 +339,7 @@ public enum ListResourceTemplates: Method {
|
|||
public init() {
|
||||
self.cursor = nil
|
||||
}
|
||||
|
||||
|
||||
public init(cursor: String) {
|
||||
self.cursor = cursor
|
||||
}
|
||||
|
|
@ -164,21 +348,43 @@ public enum ListResourceTemplates: Method {
|
|||
public struct Result: Hashable, Codable, Sendable {
|
||||
public let templates: [Resource.Template]
|
||||
public let nextCursor: String?
|
||||
/// Optional metadata about this result
|
||||
public var _meta: Metadata?
|
||||
|
||||
public init(templates: [Resource.Template], nextCursor: String? = nil) {
|
||||
public init(
|
||||
templates: [Resource.Template],
|
||||
nextCursor: String? = nil,
|
||||
_meta: Metadata? = nil
|
||||
) {
|
||||
self.templates = templates
|
||||
self.nextCursor = nextCursor
|
||||
self._meta = _meta
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
private enum CodingKeys: String, CodingKey, CaseIterable {
|
||||
case templates = "resourceTemplates"
|
||||
case nextCursor
|
||||
case _meta
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(templates, forKey: .templates)
|
||||
try container.encodeIfPresent(nextCursor, forKey: .nextCursor)
|
||||
try container.encodeIfPresent(_meta, forKey: ._meta)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
templates = try container.decode([Resource.Template].self, forKey: .templates)
|
||||
nextCursor = try container.decodeIfPresent(String.self, forKey: .nextCursor)
|
||||
_meta = try container.decodeIfPresent(Metadata.self, forKey: ._meta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// When the list of available resources changes, servers that declared the listChanged capability SHOULD send a notification.
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/server/resources/#list-changed-notification
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2025-06-18/server/resources/#list-changed-notification
|
||||
public struct ResourceListChangedNotification: Notification {
|
||||
public static let name: String = "notifications/resources/list_changed"
|
||||
|
||||
|
|
@ -186,7 +392,7 @@ public struct ResourceListChangedNotification: Notification {
|
|||
}
|
||||
|
||||
/// Clients can subscribe to specific resources and receive notifications when they change.
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/server/resources/#subscriptions
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2025-06-18/server/resources/#subscriptions
|
||||
public enum ResourceSubscribe: Method {
|
||||
public static let name: String = "resources/subscribe"
|
||||
|
||||
|
|
@ -197,8 +403,20 @@ public enum ResourceSubscribe: Method {
|
|||
public typealias Result = Empty
|
||||
}
|
||||
|
||||
/// Sent from the client to request cancellation of resources/updated notifications from the server. This should follow a previous resources/subscribe request.
|
||||
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-06-18/schema#unsubscriberequest
|
||||
public enum ResourceUnsubscribe: Method {
|
||||
public static let name: String = "resources/unsubscribe"
|
||||
|
||||
public struct Parameters: Hashable, Codable, Sendable {
|
||||
public let uri: String
|
||||
}
|
||||
|
||||
public typealias Result = Empty
|
||||
}
|
||||
|
||||
/// When a resource changes, servers that declared the updated capability SHOULD send a notification to subscribed clients.
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/server/resources/#subscriptions
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2025-06-18/server/resources/#subscriptions
|
||||
public struct ResourceUpdatedNotification: Notification {
|
||||
public static let name: String = "notifications/resources/updated"
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue