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
|
|
@ -7,32 +7,80 @@ import Foundation
|
|||
/// Clients can discover available prompts, retrieve their contents,
|
||||
/// and provide arguments to customize them.
|
||||
///
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/server/prompts/
|
||||
/// - SeeAlso: https://modelcontextprotocol.io/specification/2025-11-25/server/prompts/
|
||||
public struct Prompt: Hashable, Codable, Sendable {
|
||||
/// The prompt name
|
||||
public let name: String
|
||||
/// A human-readable prompt title
|
||||
public let title: String?
|
||||
/// The prompt description
|
||||
public let description: String?
|
||||
/// The prompt arguments
|
||||
public let arguments: [Argument]?
|
||||
/// Optional set of sized icons that the client can display in a user interface
|
||||
public var icons: [Icon]?
|
||||
/// Optional metadata about this prompt
|
||||
public var _meta: Metadata?
|
||||
|
||||
public init(name: String, description: String? = nil, arguments: [Argument]? = nil) {
|
||||
public init(
|
||||
name: String,
|
||||
title: String? = nil,
|
||||
description: String? = nil,
|
||||
arguments: [Argument]? = nil,
|
||||
icons: [Icon]? = nil,
|
||||
meta: Metadata? = nil
|
||||
) {
|
||||
self.name = name
|
||||
self.title = title
|
||||
self.description = description
|
||||
self.arguments = arguments
|
||||
self.icons = icons
|
||||
self._meta = meta
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case name, title, description, arguments, icons, _meta
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(title, forKey: .title)
|
||||
try container.encodeIfPresent(description, forKey: .description)
|
||||
try container.encodeIfPresent(arguments, forKey: .arguments)
|
||||
try container.encodeIfPresent(icons, forKey: . icons)
|
||||
try container.encodeIfPresent(_meta, forKey: . _meta)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
name = try container.decode(String.self, forKey: .name)
|
||||
title = try container.decodeIfPresent(String.self, forKey: .title)
|
||||
description = try container.decodeIfPresent(String.self, forKey: .description)
|
||||
arguments = try container.decodeIfPresent([Argument].self, forKey: .arguments)
|
||||
icons = try container.decodeIfPresent([Icon].self, forKey: . icons)
|
||||
_meta = try container.decodeIfPresent(Metadata.self, forKey: . _meta)
|
||||
}
|
||||
|
||||
/// An argument for a prompt
|
||||
public struct Argument: Hashable, Codable, Sendable {
|
||||
/// The argument name
|
||||
public let name: String
|
||||
/// A human-readable argument title
|
||||
public let title: String?
|
||||
/// The argument description
|
||||
public let description: String?
|
||||
/// Whether the argument is required
|
||||
public let required: Bool?
|
||||
|
||||
public init(name: String, description: String? = nil, required: Bool? = nil) {
|
||||
public init(
|
||||
name: String,
|
||||
title: String? = nil,
|
||||
description: String? = nil,
|
||||
required: Bool? = nil
|
||||
) {
|
||||
self.name = name
|
||||
self.title = title
|
||||
self.description = description
|
||||
self.required = required
|
||||
}
|
||||
|
|
@ -86,8 +134,8 @@ public struct Prompt: Hashable, Codable, Sendable {
|
|||
case image(data: String, mimeType: String)
|
||||
/// Audio content
|
||||
case audio(data: String, mimeType: String)
|
||||
/// Embedded resource content
|
||||
case resource(uri: String, mimeType: String, text: String?, blob: String?)
|
||||
/// Embedded resource content (EmbeddedResource from spec)
|
||||
case resource(resource: Resource.Content, annotations: Resource.Annotations? = nil, _meta: Metadata? = nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -95,25 +143,30 @@ public struct Prompt: Hashable, Codable, Sendable {
|
|||
public struct Reference: Hashable, Codable, Sendable {
|
||||
/// The prompt reference name
|
||||
public let name: String
|
||||
/// A human-readable prompt title
|
||||
public let title: String?
|
||||
|
||||
public init(name: String) {
|
||||
public init(name: String, title: String? = nil) {
|
||||
self.name = name
|
||||
self.title = title
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case type, name
|
||||
case type, name, title
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode("ref/prompt", forKey: .type)
|
||||
try container.encode(name, forKey: .name)
|
||||
try container.encodeIfPresent(title, forKey: .title)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
_ = try container.decode(String.self, forKey: .type)
|
||||
name = try container.decode(String.self, forKey: .name)
|
||||
title = try container.decodeIfPresent(String.self, forKey: .title)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -122,7 +175,7 @@ public struct Prompt: Hashable, Codable, Sendable {
|
|||
|
||||
extension Prompt.Message.Content: Codable {
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case type, text, data, mimeType, uri, blob
|
||||
case type, text, data, mimeType, resource, annotations, _meta
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
|
|
@ -140,12 +193,11 @@ extension Prompt.Message.Content: Codable {
|
|||
try container.encode("audio", forKey: .type)
|
||||
try container.encode(data, forKey: .data)
|
||||
try container.encode(mimeType, forKey: .mimeType)
|
||||
case .resource(let uri, let mimeType, let text, let blob):
|
||||
case .resource(let resourceContent, let annotations, let _meta):
|
||||
try container.encode("resource", forKey: .type)
|
||||
try container.encode(uri, forKey: .uri)
|
||||
try container.encode(mimeType, forKey: .mimeType)
|
||||
try container.encodeIfPresent(text, forKey: .text)
|
||||
try container.encodeIfPresent(blob, forKey: .blob)
|
||||
try container.encode(resourceContent, forKey: .resource)
|
||||
try container.encodeIfPresent(annotations, forKey: .annotations)
|
||||
try container.encodeIfPresent(_meta, forKey: ._meta)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -166,11 +218,10 @@ extension Prompt.Message.Content: Codable {
|
|||
let mimeType = try container.decode(String.self, forKey: .mimeType)
|
||||
self = .audio(data: data, mimeType: mimeType)
|
||||
case "resource":
|
||||
let uri = try container.decode(String.self, forKey: .uri)
|
||||
let mimeType = try container.decode(String.self, forKey: .mimeType)
|
||||
let text = try container.decodeIfPresent(String.self, forKey: .text)
|
||||
let blob = try container.decodeIfPresent(String.self, forKey: .blob)
|
||||
self = .resource(uri: uri, mimeType: mimeType, text: text, blob: blob)
|
||||
let resourceContent = try container.decode(Resource.Content.self, forKey: .resource)
|
||||
let annotations = try container.decodeIfPresent(Resource.Annotations.self, forKey: .annotations)
|
||||
let _meta = try container.decodeIfPresent(Metadata.self, forKey: ._meta)
|
||||
self = .resource(resource: resourceContent, annotations: annotations, _meta: _meta)
|
||||
default:
|
||||
throw DecodingError.dataCorruptedError(
|
||||
forKey: .type,
|
||||
|
|
@ -199,7 +250,7 @@ extension Prompt.Message.Content: ExpressibleByStringInterpolation {
|
|||
// MARK: -
|
||||
|
||||
/// To retrieve available prompts, clients send a `prompts/list` request.
|
||||
/// - SeeAlso: https://spec.modelcontextprotocol.io/specification/2024-11-05/server/prompts/#listing-prompts
|
||||
/// - SeeAlso: https://modelcontextprotocol.io/specification/2024-11-05/server/prompts/#listing-prompts
|
||||
public enum ListPrompts: Method {
|
||||
public static let name: String = "prompts/list"
|
||||
|
||||
|
|
@ -216,12 +267,36 @@ public enum ListPrompts: Method {
|
|||
}
|
||||
|
||||
public struct Result: Hashable, Codable, Sendable {
|
||||
public let prompts: [Prompt]
|
||||
public let nextCursor: String?
|
||||
let prompts: [Prompt]
|
||||
let nextCursor: String?
|
||||
var _meta: Metadata?
|
||||
|
||||
public init(prompts: [Prompt], nextCursor: String? = nil) {
|
||||
public init(
|
||||
prompts: [Prompt],
|
||||
nextCursor: String? = nil,
|
||||
_meta: Metadata? = nil
|
||||
) {
|
||||
self.prompts = prompts
|
||||
self.nextCursor = nextCursor
|
||||
self._meta = _meta
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey, CaseIterable {
|
||||
case prompts, nextCursor, _meta
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(prompts, forKey: .prompts)
|
||||
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)
|
||||
prompts = try container.decode([Prompt].self, forKey: .prompts)
|
||||
nextCursor = try container.decodeIfPresent(String.self, forKey: .nextCursor)
|
||||
_meta = try container.decodeIfPresent(Metadata.self, forKey: ._meta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -245,10 +320,35 @@ public enum GetPrompt: Method {
|
|||
public struct Result: Hashable, Codable, Sendable {
|
||||
public let description: String?
|
||||
public let messages: [Prompt.Message]
|
||||
/// Optional metadata about this result
|
||||
public var _meta: Metadata?
|
||||
|
||||
public init(description: String?, messages: [Prompt.Message]) {
|
||||
public init(
|
||||
description: String? = nil,
|
||||
messages: [Prompt.Message],
|
||||
_meta: Metadata? = nil
|
||||
) {
|
||||
self.description = description
|
||||
self.messages = messages
|
||||
self._meta = _meta
|
||||
}
|
||||
|
||||
private enum CodingKeys: String, CodingKey, CaseIterable {
|
||||
case description, messages, _meta
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encodeIfPresent(description, forKey: .description)
|
||||
try container.encode(messages, forKey: .messages)
|
||||
try container.encodeIfPresent(_meta, forKey: ._meta)
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
description = try container.decodeIfPresent(String.self, forKey: .description)
|
||||
messages = try container.decode([Prompt.Message].self, forKey: .messages)
|
||||
_meta = try container.decodeIfPresent(Metadata.self, forKey: ._meta)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue