|
1 | | -import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js"; |
2 | | -import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
3 | | -import path from "path"; |
4 | | -import * as z from "zod"; |
5 | | -import { OpenAPIParser } from "./openapi-parser"; |
6 | | -import { CreateMessageResultSchema } from "@modelcontextprotocol/sdk/types.js"; |
7 | | -import { paystackClient } from "./paystack-client"; |
8 | | - |
9 | | -// const PAYSTACK_BASE_URL = process.env.PAYSTACK_BASE_URL || "https://api.paystack.co"; |
10 | | -// const USER_AGENT = process.env.USER_AGENT || "paystack-mcp/1.0"; |
11 | | - |
12 | | -// Create server instance |
13 | | -const server = new McpServer({ |
14 | | - name: "paystack", |
15 | | - version: "1.0.0", |
16 | | -}); |
17 | | - |
18 | | -const oasPath = path.join(__dirname, "./", "data/paystack.openapi.yaml"); |
19 | | -const openapi = new OpenAPIParser(oasPath); |
20 | | - |
21 | | -async function initializeServer() { |
22 | | - // Parse OpenAPI spec before registering tools |
23 | | - await openapi.parse(); |
24 | | - |
25 | | - server.registerTool( |
26 | | - "get_paystack_operation", |
27 | | - { |
28 | | - description: "Get Paystack API operation details by operation ID", |
29 | | - annotations: { |
30 | | - title: "Get endpoint details by operation ID", |
31 | | - }, |
32 | | - inputSchema: { |
33 | | - operation_id: z |
34 | | - .string() |
35 | | - .describe("The operation ID of the Paystack API endpoint"), |
36 | | - } |
37 | | - }, |
38 | | - async ({ operation_id }) => { |
39 | | - |
40 | | - try { |
41 | | - const operation = openapi.getOperationById(operation_id.trim()); |
42 | | - console.error("Operation: ", operation) |
43 | | - |
44 | | - if (!operation) { |
45 | | - return { |
46 | | - content: [ |
47 | | - { |
48 | | - type: "text", |
49 | | - text: `Operation with ID ${operation_id} not found.`, |
50 | | - }, |
51 | | - ] |
52 | | - } |
53 | | - } |
54 | | - |
55 | | - return { |
56 | | - content: [ |
57 | | - { |
58 | | - type: "text", |
59 | | - text: JSON.stringify(operation, null, 2), |
60 | | - mimeType: "application/json", |
61 | | - }, |
62 | | - ] |
63 | | - } |
64 | | - } catch { |
65 | | - return { |
66 | | - content: [ |
67 | | - { |
68 | | - type: "text", |
69 | | - text: `Operation with ID ${operation_id} not found.`, |
70 | | - }, |
71 | | - ] |
72 | | - } |
73 | | - } |
74 | | - } |
75 | | - ) |
76 | | - |
77 | | - server.registerTool( |
78 | | - "make_paystack_request", |
79 | | - { |
80 | | - description: `Make a Paystack API request using the details of the operation. Be sure |
81 | | - to get all operation details including method, path path parameters, query parameters, |
82 | | - and request body before making a call.`, |
83 | | - annotations: { |
84 | | - title: "Get endpoint details by operation ID", |
85 | | - }, |
86 | | - inputSchema: { |
87 | | - request: z.object({ |
88 | | - method: z.string().describe("HTTP method of the API request"), |
89 | | - path: z.string().describe("Path of the API request"), |
90 | | - data: z.looseObject({}).optional().describe("Request data"), |
91 | | - }) |
92 | | - } |
93 | | - }, |
94 | | - async ({ request }) => { |
95 | | - try { |
96 | | - console.error("Request received:", request.method); |
97 | | - console.error("Request received:", request.path); |
98 | | - console.error("Request received:", request.data); |
99 | | - |
100 | | - const response = await paystackClient.makeRequest( |
101 | | - request.method, |
102 | | - request.path, |
103 | | - request.data |
104 | | - ) |
105 | | - console.error("response: ", response) |
106 | | - |
107 | | - return { |
108 | | - content: [ |
109 | | - { |
110 | | - type: "text", |
111 | | - text: JSON.stringify(response, null, 2), |
112 | | - mimeType: "application/json", |
113 | | - }, |
114 | | - ] |
115 | | - } |
116 | | - } catch { |
117 | | - return { |
118 | | - content: [ |
119 | | - { |
120 | | - type: "text", |
121 | | - text: `Unable to make request.`, |
122 | | - }, |
123 | | - ] |
124 | | - } |
125 | | - } |
126 | | - } |
127 | | - ) |
128 | | - |
129 | | - server.registerTool( |
130 | | - "get_operation_guided", |
131 | | - { |
132 | | - description: "Get Paystack API operation details from user input", |
133 | | - annotations: { |
134 | | - title: "Get endpoint details from user input", |
135 | | - readOnlyHint: true, |
136 | | - destructiveHint: false, |
137 | | - idempotentHint: false, |
138 | | - openWorldHint: false, |
139 | | - }, |
140 | | - }, |
141 | | - async () => { |
142 | | - const res = await server.server.request({ |
143 | | - method: "sampling/createMessage", |
144 | | - params: { |
145 | | - messages: [{ |
146 | | - role: "user", |
147 | | - content: [ |
148 | | - { |
149 | | - type: "text", |
150 | | - text: `Review the OpenAPI specification and infer the operation ID of the |
151 | | - Paystack API endpoint from the user input. |
152 | | - For example if the user's input is: 'I want to create a new customer in Paystack.' |
153 | | - review the OpenAPI spec and respond with the most logical operationId: |
154 | | - which is 'customer_create'. Return just the operationId in your response.`, |
155 | | - }, |
156 | | - ], |
157 | | - }], |
158 | | - maxTokens: 1024, |
159 | | - } |
160 | | - }, CreateMessageResultSchema) |
161 | | - |
162 | | - if (res.content.type !== "text") { |
163 | | - return { |
164 | | - content: [ |
165 | | - { |
166 | | - type: "text", |
167 | | - text: `Could not infer operation ID from user input.`, |
168 | | - } |
169 | | - ] |
170 | | - } |
171 | | - } |
172 | | - |
173 | | - try { |
174 | | - const operation_id = res.content.text.trim(); |
175 | | - const operation = openapi.getOperationById(operation_id); |
176 | | - |
177 | | - if (!operation) { |
178 | | - return { |
179 | | - content: [ |
180 | | - { |
181 | | - type: "text", |
182 | | - text: `Operation with ID ${operation_id} not found.`, |
183 | | - }, |
184 | | - ], |
185 | | - isError: true, |
186 | | - } |
187 | | - } |
188 | | - |
189 | | - return { |
190 | | - content: [ |
191 | | - { |
192 | | - type: "text", |
193 | | - text: JSON.stringify(operation, null, 2), |
194 | | - mimeType: "application/json", |
195 | | - }, |
196 | | - ] |
197 | | - } |
198 | | - } catch { |
199 | | - return { |
200 | | - content: [ |
201 | | - { |
202 | | - type: "text", |
203 | | - text: `Operation with ID cannot be infered.`, |
204 | | - }, |
205 | | - ] |
206 | | - } |
207 | | - } |
208 | | - } |
209 | | - ) |
210 | | - |
211 | | - server.registerResource( |
212 | | - "operation-list", |
213 | | - new ResourceTemplate("openapi://operations/list", { list: undefined }), |
214 | | - { |
215 | | - description: "Retrieve all operation IDs", |
216 | | - title: "List of Paystack API operation IDs", |
217 | | - mimeType: "text/plain", |
218 | | - }, |
219 | | - async (uri) => { |
220 | | - // await openapi.parse(); |
221 | | - const operations = openapi.getOperations(); |
222 | | - const operationIds = Object.keys(operations); |
223 | | - |
224 | | - if (operationIds.length === 0) { |
225 | | - return { |
226 | | - contents: [ |
227 | | - { |
228 | | - uri: uri.href, |
229 | | - type: "text", |
230 | | - text: "Unable to list operations.", |
231 | | - mimeType: "text/plain", |
232 | | - }, |
233 | | - ] |
234 | | - } |
235 | | - } |
236 | | - |
237 | | - return { |
238 | | - contents: [ |
239 | | - { |
240 | | - uri: uri.href, |
241 | | - type: "text", |
242 | | - text: operationIds.join("\n"), |
243 | | - mimeType: "text/plain", |
244 | | - }, |
245 | | - ] |
246 | | - } |
247 | | - } |
248 | | - ) |
249 | | -} |
| 1 | +import { startServer } from "./server"; |
250 | 2 |
|
251 | 3 | async function main() { |
252 | | - await initializeServer(); |
253 | | - const transport = new StdioServerTransport(); |
254 | | - await server.connect(transport); |
255 | | - console.error("Paystack MCP Server running on stdio..."); |
| 4 | + await startServer(); |
256 | 5 | } |
257 | 6 |
|
258 | | - |
259 | 7 | main().catch((error) => { |
260 | 8 | console.error("Fatal error in main():", error); |
261 | 9 | process.exit(1); |
|
0 commit comments