Skip to content

Commit 54244d2

Browse files
committed
updated documentation
1 parent 552cc72 commit 54244d2

File tree

4 files changed

+89
-32
lines changed

4 files changed

+89
-32
lines changed

Sources/SwiftMCP/Models/MCPTool.swift

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ extension MCPTool: Codable {
6767
*/
6868
extension MCPTool {
6969
/**
70-
Enriches the provided arguments dictionary with default values for missing parameters.
70+
Enriches a dictionary of arguments with default values for any missing parameters.
7171

7272
- Parameters:
7373
- arguments: The original arguments dictionary
7474
- object: The object containing the function metadata
75-
- functionName: The name of the function to get default values for
7675

7776
- Returns: A new dictionary with default values added for missing parameters
77+
- Throws: MCPToolError if required parameters are missing or if parameter conversion fails
7878
*/
7979
public func enrichArguments(_ arguments: [String: Any], forObject object: Any) throws -> [String: Any] {
8080
// Use the provided function name or fall back to the tool's name
@@ -130,16 +130,26 @@ extension MCPTool {
130130
Extension to make JSONSchema conform to Codable
131131
*/
132132
extension JSONSchema: Codable {
133-
// MARK: - Codable Implementation
134-
133+
/// Coding keys for JSONSchema encoding and decoding
135134
private enum CodingKeys: String, CodingKey {
135+
/// The type of the schema (string, number, boolean, array, or object)
136136
case type
137+
/// The properties of an object schema
137138
case properties
139+
/// The required properties of an object schema
138140
case required
141+
/// A description of the schema
139142
case description
143+
/// The schema for array items
140144
case items
141145
}
142146

147+
/**
148+
Creates a new JSONSchema instance by decoding from the given decoder.
149+
150+
- Parameter decoder: The decoder to read data from
151+
- Throws: DecodingError if the data is corrupted or if an unsupported schema type is encountered
152+
*/
143153
public init(from decoder: Decoder) throws {
144154
let container = try decoder.container(keyedBy: CodingKeys.self)
145155
let type = try container.decode(String.self, forKey: .type)
@@ -173,6 +183,12 @@ extension JSONSchema: Codable {
173183
}
174184
}
175185

186+
/**
187+
Encodes this JSONSchema instance into the given encoder.
188+
189+
- Parameter encoder: The encoder to write data to
190+
- Throws: EncodingError if the data cannot be encoded
191+
*/
176192
public func encode(to encoder: Encoder) throws {
177193
var container = encoder.container(keyedBy: CodingKeys.self)
178194

@@ -209,16 +225,31 @@ extension JSONSchema: Codable {
209225

210226
/**
211227
A coding key that can be initialized with any string value.
228+
Used for encoding and decoding dynamic property names in JSON schemas.
212229
*/
213230
private struct AnyCodingKey: CodingKey {
231+
/// The string value of the coding key
214232
var stringValue: String
233+
/// The integer value of the coding key, if any
215234
var intValue: Int?
216235

236+
/**
237+
Creates a coding key from a string value.
238+
239+
- Parameter stringValue: The string value for the key
240+
- Returns: A coding key, or nil if the string value is invalid
241+
*/
217242
init?(stringValue: String) {
218243
self.stringValue = stringValue
219244
self.intValue = nil
220245
}
221246

247+
/**
248+
Creates a coding key from an integer value.
249+
250+
- Parameter intValue: The integer value for the key
251+
- Returns: A coding key, or nil if the integer value is invalid
252+
*/
222253
init?(intValue: Int) {
223254
self.stringValue = String(intValue)
224255
self.intValue = intValue

Sources/SwiftMCP/Models/MCPToolMetadata.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@ public struct MCPToolMetadata: Sendable {
1717

1818
/// The return type of the function, if any
1919
public let returnType: String?
20-
21-
// an optional description of the returned value
22-
public let returnTypeDescription: String?
23-
24-
// If the tool is async
25-
public let isAsync: Bool
26-
27-
// If the tool throws
28-
public let isThrowing: Bool
2920

30-
/// An optional description of the function
21+
/// A description of what the function returns
22+
public let returnTypeDescription: String?
23+
24+
/// Whether the function is asynchronous
25+
public let isAsync: Bool
26+
27+
/// Whether the function can throw errors
28+
public let isThrowing: Bool
29+
30+
/// A description of the function's purpose
3131
public let description: String?
3232

3333
/**
34-
Creates a new tool metadata with the specified name, parameters, return type, and description.
34+
Creates a new MCPToolMetadata instance.
3535

3636
- Parameters:
3737
- name: The name of the function
38+
- description: A description of the function's purpose
3839
- parameters: The parameters of the function
3940
- returnType: The return type of the function, if any
40-
- description: An optional description of the function's purpose
41+
- returnTypeDescription: A description of what the function returns
42+
- isAsync: Whether the function is asynchronous
43+
- isThrowing: Whether the function can throw errors
4144
*/
4245
public init(name: String, description: String? = nil, parameters: [MCPToolParameterInfo], returnType: String? = nil, returnTypeDescription: String? = nil, isAsync: Bool = false, isThrowing: Bool = false) {
4346
self.name = name

Sources/SwiftMCP/OpenAPI/OpenAPISpec.swift

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,59 +4,82 @@ import Foundation
44
public struct OpenAPISpec: Codable {
55
/// Basic information about the API
66
public struct Info: Codable {
7+
/// The title of the API
78
public let title: String
9+
/// The version of the API
810
public let version: String
11+
/// A description of the API
912
public let description: String
1013
}
1114

1215
/// Server information
1316
public struct Server: Codable {
17+
/// The server URL
1418
public let url: String
19+
/// A description of the server
1520
public let description: String
1621
}
1722

1823
/// Request or response content
1924
public struct Content: Codable {
25+
/// The schema defining the content structure
2026
public let schema: JSONSchema
2127
}
2228

2329
/// Request body specification
2430
public struct RequestBody: Codable {
31+
/// Whether the request body is required
2532
public let required: Bool
33+
/// The content types and their schemas
2634
public let content: [String: Content]
2735
}
2836

2937
/// Response specification
3038
public struct Response: Codable {
39+
/// A description of the response
3140
public let description: String
41+
/// The content types and their schemas, if any
3242
public let content: [String: Content]?
3343
}
3444

3545
/// Operation (e.g., POST) specification
3646
public struct Operation: Codable {
47+
/// A brief summary of what the operation does
3748
public let summary: String
49+
/// Unique identifier for the operation
3850
public let operationId: String
51+
/// A detailed description of the operation
3952
public let description: String
53+
/// The request body specification, if any
4054
public let requestBody: RequestBody?
55+
/// The possible responses keyed by status code
4156
public let responses: [String: Response]
4257
}
4358

4459
/// Path item specification
4560
public struct PathItem: Codable {
61+
/// The POST operation specification, if any
4662
public let post: Operation?
4763
}
4864

65+
/// The OpenAPI specification version
4966
public let openapi: String
67+
/// Basic information about the API
5068
public let info: Info
69+
/// The servers where the API is available
5170
public let servers: [Server]
71+
/// The available paths and their operations
5272
public let paths: [String: PathItem]
5373

54-
/// Creates an OpenAPI specification for an MCP server
55-
/// - Parameters:
56-
/// - server: The MCP server to create the spec for
57-
/// - host: The host where the server is running
58-
/// - port: The port where the server is running
59-
public init(server: MCPServer, scheme: String, host: String) {
74+
/**
75+
Creates an OpenAPI specification for an MCP server
76+
77+
- Parameters:
78+
- server: The MCP server to create the spec for
79+
- scheme: The URL scheme to use (e.g., "http" or "https")
80+
- host: The host where the server is running
81+
*/
82+
public init(server: MCPServer, scheme: String, host: String) {
6083
self.openapi = "3.1.0"
6184
self.info = Info(
6285
title: "\(server.name) API",

Sources/SwiftMCP/Transport/HTTPLogger.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import NIOHTTP1
55
import NIOConcurrencyHelpers
66

77
/// A channel handler that logs both incoming and outgoing HTTP messages
8-
public final class HTTPLogger: ChannelDuplexHandler {
9-
public typealias InboundIn = HTTPServerRequestPart
10-
public typealias InboundOut = HTTPServerRequestPart
11-
public typealias OutboundIn = HTTPServerResponsePart
12-
public typealias OutboundOut = HTTPServerResponsePart
8+
final class HTTPLogger: ChannelDuplexHandler {
9+
typealias InboundIn = HTTPServerRequestPart
10+
typealias InboundOut = HTTPServerRequestPart
11+
typealias OutboundIn = HTTPServerResponsePart
12+
typealias OutboundOut = HTTPServerResponsePart
1313

1414
private let httpLogger = Logger(label: "com.cocoanetics.SwiftMCP.HTTP")
1515
private let lock = NIOLock()
@@ -21,7 +21,7 @@ public final class HTTPLogger: ChannelDuplexHandler {
2121
private var currentResponseBody = ""
2222

2323
/// Log incoming requests and forward them to the next handler
24-
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
24+
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
2525
let reqPart = unwrapInboundIn(data)
2626

2727
lock.withLock {
@@ -49,7 +49,7 @@ public final class HTTPLogger: ChannelDuplexHandler {
4949
}
5050

5151
/// Log outgoing responses and forward them to the next handler
52-
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
52+
func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
5353
let resPart = unwrapOutboundIn(data)
5454

5555
lock.withLock {
@@ -78,7 +78,7 @@ public final class HTTPLogger: ChannelDuplexHandler {
7878
context.write(data, promise: promise)
7979
}
8080

81-
public func flush(context: ChannelHandlerContext) {
81+
func flush(context: ChannelHandlerContext) {
8282
context.flush()
8383
}
8484

@@ -110,7 +110,7 @@ public final class HTTPLogger: ChannelDuplexHandler {
110110
httpLogger.trace("\(log)")
111111
}
112112

113-
public func handlerAdded(context: ChannelHandlerContext) {
113+
func handlerAdded(context: ChannelHandlerContext) {
114114
lock.withLock {
115115
currentRequestHead = nil
116116
currentRequestBody = ""
@@ -119,7 +119,7 @@ public final class HTTPLogger: ChannelDuplexHandler {
119119
}
120120
}
121121

122-
public func handlerRemoved(context: ChannelHandlerContext) {
122+
func handlerRemoved(context: ChannelHandlerContext) {
123123
lock.withLock {
124124
// Log any pending messages
125125
logCurrentRequest()

0 commit comments

Comments
 (0)