|
1 | 1 | import Foundation |
2 | 2 | import AnyCodable |
3 | 3 |
|
4 | | -/// Protocol defining the requirements for an MCP server |
5 | | -public protocol MCPServer: AnyObject { |
6 | | - /// Returns an array of all MCP tools defined in this type |
| 4 | +/// Protocol defining the interface for an MCP server |
| 5 | +public protocol MCPServer { |
| 6 | + /// The tools available on this server |
7 | 7 | var mcpTools: [MCPTool] { get } |
8 | 8 |
|
9 | | - /// Returns an array of all MCP resources defined in this type |
| 9 | + /// The resources available on this server |
10 | 10 | var mcpResources: [MCPResource] { get async } |
11 | | - |
12 | | - /// Returns an array of all MCP resource templates defined in this type |
13 | | - var mcpResourceTemplates: [MCPResourceTemplate] { get async } |
14 | | - |
15 | 11 |
|
16 | | - /// Calls a tool by name with the provided arguments |
| 12 | + /// The resource templates available on this server |
| 13 | + var mcpResourceTemplates: [MCPResourceTemplate] { get async } |
| 14 | + |
| 15 | + /// The name of the server |
| 16 | + var name: String { get } |
| 17 | + |
| 18 | + /// The version of the server |
| 19 | + var version: String { get } |
| 20 | + |
| 21 | + /// The description of the server |
| 22 | + var serverDescription: String? { get } |
| 23 | + |
| 24 | + /// Get a resource by its URI |
| 25 | + /// - Parameter uri: The URI of the resource to get |
| 26 | + /// - Returns: The resource content, or nil if not found |
| 27 | + func getResource(uri: URL) async throws -> MCPResourceContent? |
| 28 | + |
| 29 | + /// Call a tool by name with arguments |
17 | 30 | /// - Parameters: |
18 | 31 | /// - name: The name of the tool to call |
19 | | - /// - arguments: A dictionary of arguments to pass to the tool |
| 32 | + /// - arguments: The arguments to pass to the tool |
20 | 33 | /// - Returns: The result of the tool call |
21 | | - /// - Throws: MCPToolError if the tool doesn't exist or cannot be called |
22 | 34 | func callTool(_ name: String, arguments: [String: Any]) async throws -> Codable |
23 | 35 |
|
24 | | - /// Gets a resource by URI |
25 | | - /// - Parameter uri: The URI of the resource to get |
26 | | - /// - Returns: The resource content, or nil if the resource doesn't exist |
27 | | - /// - Throws: MCPResourceError if there's an error getting the resource |
28 | | - func getResource(uri: URL) async throws -> MCPResourceContent? |
29 | | - |
30 | | - /// Handles a JSON-RPC request |
| 36 | + /// Handle a JSON-RPC request |
31 | 37 | /// - Parameter request: The JSON-RPC request to handle |
32 | 38 | /// - Returns: The response as a string, or nil if no response should be sent |
33 | 39 | func handleRequest(_ request: JSONRPCMessage) async -> JSONRPCMessage? |
@@ -134,45 +140,53 @@ public extension MCPServer { |
134 | 140 | /// Handles a tool call request |
135 | 141 | /// - Parameter request: The JSON-RPC request for a tool call |
136 | 142 | /// - Returns: The response as a string, or nil if no response should be sent |
137 | | - private func handleToolCall(_ request: JSONRPCMessage) async -> JSONRPCMessage? { |
138 | | - guard let params = request.params, |
139 | | - let toolName = params["name"]?.value as? String else { |
140 | | - // Invalid request: missing tool name |
141 | | - return nil |
142 | | - } |
143 | | - |
144 | | - // Extract arguments from the request |
145 | | - let arguments = (params["arguments"]?.value as? [String: Any]) ?? [:] |
146 | | - |
147 | | - // Call the appropriate wrapper method based on the tool name |
148 | | - do { |
149 | | - let result = try await self.callTool(toolName, arguments: arguments) |
150 | | - let responseText: String |
151 | | - |
152 | | - // Use Mirror to check if the result is Void |
153 | | - let mirror = Mirror(reflecting: result) |
154 | | - if mirror.displayStyle == .tuple && mirror.children.isEmpty { |
155 | | - responseText = "" // Convert Void to empty string |
156 | | - } else { |
157 | | - responseText = "\(result)" |
158 | | - } |
159 | | - |
160 | | - var response = JSONRPCMessage() |
161 | | - response.id = request.id |
162 | | - response.result = [ |
163 | | - "content": AnyCodable([ |
164 | | - ["type": "text", "text": responseText] |
165 | | - ]) |
166 | | - ] |
167 | | - return response |
168 | | - |
169 | | - } catch { |
170 | | - var response = JSONRPCMessage() |
171 | | - response.id = request.id |
172 | | - response.error = .init(code: -32000, message: error.localizedDescription) |
173 | | - return response |
174 | | - } |
175 | | - } |
| 143 | + private func handleToolCall(_ request: JSONRPCMessage) async -> JSONRPCMessage? { |
| 144 | + guard let params = request.params, |
| 145 | + let toolName = params["name"]?.value as? String else { |
| 146 | + // Invalid request: missing tool name |
| 147 | + return nil |
| 148 | + } |
| 149 | + |
| 150 | + // Extract arguments from the request |
| 151 | + let arguments = (params["arguments"]?.value as? [String: Any]) ?? [:] |
| 152 | + |
| 153 | + // Call the appropriate wrapper method based on the tool name |
| 154 | + do { |
| 155 | + let result = try await self.callTool(toolName, arguments: arguments) |
| 156 | + let responseText: String |
| 157 | + |
| 158 | + // Use Mirror to check if the result is Void |
| 159 | + let mirror = Mirror(reflecting: result) |
| 160 | + if mirror.displayStyle == .tuple && mirror.children.isEmpty { |
| 161 | + responseText = "" // Convert Void to empty string |
| 162 | + } else { |
| 163 | + responseText = "\(result)" |
| 164 | + } |
| 165 | + |
| 166 | + var response = JSONRPCMessage() |
| 167 | + response.jsonrpc = "2.0" |
| 168 | + response.id = request.id |
| 169 | + response.result = [ |
| 170 | + "content": AnyCodable([ |
| 171 | + ["type": "text", "text": responseText] |
| 172 | + ]), |
| 173 | + "isError": AnyCodable(false) |
| 174 | + ] |
| 175 | + return response |
| 176 | + |
| 177 | + } catch { |
| 178 | + var response = JSONRPCMessage() |
| 179 | + response.jsonrpc = "2.0" |
| 180 | + response.id = request.id |
| 181 | + response.result = [ |
| 182 | + "content": AnyCodable([ |
| 183 | + ["type": "text", "text": error.localizedDescription] |
| 184 | + ]), |
| 185 | + "isError": AnyCodable(true) |
| 186 | + ] |
| 187 | + return response |
| 188 | + } |
| 189 | + } |
176 | 190 |
|
177 | 191 | /// Function to log a message to stderr |
178 | 192 | func logToStderr(_ message: String) { |
|
0 commit comments