|
| 1 | +#!/usr/bin/env node |
| 2 | +import "tsx"; |
| 3 | +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; |
| 4 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 5 | +import { |
| 6 | + CallToolRequestSchema, |
| 7 | + ListToolsRequestSchema, |
| 8 | +} from "@modelcontextprotocol/sdk/types.js"; |
| 9 | +const { HttpClient, OpenAPIToMCPConverter } = await import( |
| 10 | + "openapi-mcp-server" |
| 11 | +); |
| 12 | + |
| 13 | +/** |
| 14 | + * Error thrown by HttpClient when a request fails |
| 15 | + * @extends Error |
| 16 | + */ |
| 17 | +class HttpClientError extends Error { |
| 18 | + constructor( |
| 19 | + /** @param {string} message - Error message */ |
| 20 | + message, |
| 21 | + /** @param {number} status - HTTP status code */ |
| 22 | + status, |
| 23 | + /** @param {*} data - Response data */ |
| 24 | + data, |
| 25 | + /** @param {Headers} [headers] - Response headers */ |
| 26 | + headers, |
| 27 | + ) { |
| 28 | + super(`${status} ${message}`); |
| 29 | + this.name = "HttpClientError"; |
| 30 | + this.status = status; |
| 31 | + this.data = data; |
| 32 | + this.headers = headers; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * @typedef {import("openapi-mcp-server").OpenAPIV3.Document} OpenApiSpec |
| 38 | + * @type {{ client: HttpClient, spec: OpenApiSpec }[]} |
| 39 | + */ |
| 40 | +const openApiSpecs = ( |
| 41 | + await Promise.allSettled([ |
| 42 | + fetch("https://gi.rss3.io/docs/openapi.json").then(async (res) => { |
| 43 | + if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`); |
| 44 | + return res.json(); |
| 45 | + }), |
| 46 | + fetch("https://ai.rss3.io/openapi.json").then(async (res) => { |
| 47 | + if (!res.ok) throw new Error(`HTTP error! status: ${res.status}`); |
| 48 | + return res.json(); |
| 49 | + }), |
| 50 | + ]).then((results) => { |
| 51 | + return results.map((result) => { |
| 52 | + if (result.status === "fulfilled") { |
| 53 | + const client = new HttpClient( |
| 54 | + { |
| 55 | + baseUrl: result.value.servers[0].url, |
| 56 | + }, |
| 57 | + result.value, |
| 58 | + ); |
| 59 | + return { |
| 60 | + spec: result.value, |
| 61 | + client, |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + console.error("Failed to fetch openapi spec", result.reason); |
| 66 | + return null; |
| 67 | + }); |
| 68 | + }) |
| 69 | +).filter(Boolean); |
| 70 | + |
| 71 | +const converterWithClients = openApiSpecs.map((o) => { |
| 72 | + const converter = new OpenAPIToMCPConverter(o.spec); |
| 73 | + return { |
| 74 | + converter, |
| 75 | + client: o.client, |
| 76 | + }; |
| 77 | +}); |
| 78 | +const mcpToolWithClients = converterWithClients.map((cwc) => { |
| 79 | + const mcpTools = cwc.converter.convertToMCPTools(); |
| 80 | + return { |
| 81 | + mcpTools, |
| 82 | + client: cwc.client, |
| 83 | + }; |
| 84 | +}); |
| 85 | + |
| 86 | +// implement server |
| 87 | +const server = new Server( |
| 88 | + { |
| 89 | + name: "rss3", |
| 90 | + version: "0.1.0", |
| 91 | + }, |
| 92 | + { |
| 93 | + capabilities: { |
| 94 | + tools: {}, |
| 95 | + }, |
| 96 | + }, |
| 97 | +); |
| 98 | + |
| 99 | +// handle tool listing |
| 100 | +server.setRequestHandler(ListToolsRequestSchema, async () => { |
| 101 | + console.error("list tools"); |
| 102 | + /** |
| 103 | + * @typedef {import("@modelcontextprotocol/sdk/types.js").Tool} Tool |
| 104 | + * @type {Tool[]} |
| 105 | + */ |
| 106 | + const tools = []; |
| 107 | + |
| 108 | + for (const mcpToolWithClient of mcpToolWithClients) { |
| 109 | + for (const [toolName, def] of Object.entries( |
| 110 | + mcpToolWithClient.mcpTools.tools, |
| 111 | + )) { |
| 112 | + for (const method of def.methods) { |
| 113 | + console.error("method", method); |
| 114 | + const toolNameWithMethod = `${toolName}-${method.name}`; |
| 115 | + const truncatedToolName = toolNameWithMethod.slice(0, 64); |
| 116 | + const trimmedDescription = method.description.split("Error")[0].trim(); |
| 117 | + tools.push({ |
| 118 | + name: truncatedToolName, |
| 119 | + description: trimmedDescription, |
| 120 | + inputSchema: { |
| 121 | + type: "object", |
| 122 | + properties: {}, |
| 123 | + }, |
| 124 | + }); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + tools.unshift({ |
| 130 | + name: "API-get-input-schema", |
| 131 | + description: |
| 132 | + "Get the input schema for a given API. We should always use this tool to get the input schema for a given API before calling the API.", |
| 133 | + inputSchema: { |
| 134 | + type: "object", |
| 135 | + properties: { |
| 136 | + toolName: { |
| 137 | + type: "string", |
| 138 | + description: "The name of the tool to get the input schema for", |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }); |
| 143 | + |
| 144 | + console.error("tools", tools); |
| 145 | + |
| 146 | + return { tools }; |
| 147 | +}); |
| 148 | + |
| 149 | +// handle tool calling |
| 150 | +server.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 151 | + // console.error("call tool", request.params); |
| 152 | + const { name, arguments: params } = request.params; |
| 153 | + |
| 154 | + console.error("name", name); |
| 155 | + |
| 156 | + if (name === "API-get-input-schema") { |
| 157 | + for (const mcpToolWithClient of mcpToolWithClients) { |
| 158 | + for (const [toolName, def] of Object.entries( |
| 159 | + mcpToolWithClient.mcpTools.tools, |
| 160 | + )) { |
| 161 | + for (const method of def.methods) { |
| 162 | + const toolNameWithMethod = `${toolName}-${method.name}`; |
| 163 | + const truncatedToolName = toolNameWithMethod.slice(0, 64); |
| 164 | + if (truncatedToolName === params.toolName) { |
| 165 | + return { |
| 166 | + content: [ |
| 167 | + { type: "text", text: JSON.stringify(method.inputSchema) }, |
| 168 | + ], |
| 169 | + }; |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + throw new Error(`Method ${params.toolName} not found`); |
| 175 | + } |
| 176 | + |
| 177 | + // find operation |
| 178 | + const mcpToolWithClient = mcpToolWithClients.find( |
| 179 | + (t) => t.mcpTools.openApiLookup[name], |
| 180 | + ); |
| 181 | + if (!mcpToolWithClient) { |
| 182 | + throw new Error(`Method ${name} not found`); |
| 183 | + } |
| 184 | + |
| 185 | + const operation = mcpToolWithClient.mcpTools.openApiLookup[name]; |
| 186 | + |
| 187 | + // execute |
| 188 | + try { |
| 189 | + const response = await mcpToolWithClient.client.executeOperation( |
| 190 | + operation, |
| 191 | + params, |
| 192 | + ); |
| 193 | + return { |
| 194 | + content: [ |
| 195 | + { |
| 196 | + type: "text", // currently this is the only type that seems to be used by mcp server |
| 197 | + text: JSON.stringify(response.data), // TODO: pass through the http status code text? |
| 198 | + }, |
| 199 | + ], |
| 200 | + }; |
| 201 | + } catch (error) { |
| 202 | + console.error("Error in tool call", error); |
| 203 | + if (error instanceof HttpClientError) { |
| 204 | + console.error( |
| 205 | + "HttpClientError encountered, returning structured error", |
| 206 | + error, |
| 207 | + ); |
| 208 | + const data = error.data?.response?.data ?? error.data ?? {}; |
| 209 | + return { |
| 210 | + content: [ |
| 211 | + { |
| 212 | + type: "text", |
| 213 | + text: JSON.stringify({ |
| 214 | + status: "error", // TODO: get this from http status code? |
| 215 | + ...(typeof data === "object" ? data : { data: data }), |
| 216 | + }), |
| 217 | + }, |
| 218 | + ], |
| 219 | + }; |
| 220 | + } |
| 221 | + throw error; |
| 222 | + } |
| 223 | +}); |
| 224 | + |
| 225 | +// run server |
| 226 | +const transport = new StdioServerTransport(); |
| 227 | +await server.connect(transport); |
| 228 | +console.error("RSS3 MCP Server running on stdio"); |
0 commit comments