@letoribo/mcp-graphql-enhanced 2.1.7 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +143 -100
- package/dist/index.d.ts +1 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +122 -29
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,110 +1,153 @@
|
|
|
1
|
-
# mcp-graphql-enhanced
|
|
2
|
-
|
|
3
|
-
An
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
# **mcp-graphql-enhanced**
|
|
2
|
+
|
|
3
|
+
An enhanced MCP (Model Context Protocol) server for GraphQL that fixes real-world interoperability issues between LLMs and GraphQL APIs.
|
|
4
|
+
Drop-in replacement for mcp-graphql — with dynamic headers, robust variables parsing, and zero breaking changes.
|
|
5
|
+
|
|
6
|
+
## **✨ Key Enhancements**
|
|
7
|
+
|
|
8
|
+
* ✅ **Dual Transport** — Supports both **STDIO** (for local CLI/client tools) and **HTTP/JSON-RPC** (for external/browser clients).
|
|
9
|
+
* ✅ **Dynamic headers** — pass Authorization, X-API-Key, etc., via tool arguments (no config restarts)
|
|
10
|
+
* ✅ **Robust variables parsing** — fixes “Query variables must be a null or an object” error
|
|
11
|
+
* ✅ **Filtered introspection** — request only specific types (e.g., typeNames: \["Query", "User"\]) to reduce LLM context noise
|
|
12
|
+
* ✅ **Full MCP compatibility** — works with **Claude Desktop**, **Cursor**, **Glama**
|
|
13
|
+
* ✅ **Secure by default** — mutations disabled unless explicitly enabled
|
|
14
|
+
|
|
15
|
+
## **💻 HTTP / Dual Transport**
|
|
16
|
+
|
|
17
|
+
This server now runs in **dual transport mode**, supporting both the standard **STDIO** communication (used by most MCP clients) and a new **HTTP JSON-RPC** endpoint on port 6274\.
|
|
18
|
+
|
|
19
|
+
This allows external systems, web applications, and direct curl commands to access the server's tools.
|
|
20
|
+
|
|
21
|
+
| Endpoint | Method | Description |
|
|
22
|
+
| :---- | :---- | :---- |
|
|
23
|
+
| /mcp | POST | The main JSON-RPC endpoint for tool execution. |
|
|
24
|
+
| /health | GET | Simple health check, returns { status: 'ok' }. |
|
|
25
|
+
|
|
26
|
+
### **Testing the HTTP Endpoint**
|
|
27
|
+
|
|
28
|
+
You can test the endpoint using curl as long as the server is running (e.g., via npm run dev):
|
|
29
|
+
|
|
30
|
+
\# Test the health check
|
|
31
|
+
curl http://localhost:6274/health
|
|
32
|
+
|
|
33
|
+
\# Test the query tool via JSON-RPC
|
|
34
|
+
curl \-X POST http://localhost:6274/mcp \-H "Content-Type: application/json" \-d '{"jsonrpc":"2.0","method":"query-graphql","params":{"query":"query { \_\_typename }"},"id":1}'
|
|
35
|
+
|
|
36
|
+
## **🔍 Filtered Introspection (New\!)**
|
|
37
|
+
|
|
38
|
+
Avoid 50k-line schema dumps. Ask for only what you need: @introspect-schema typeNames \["Query", "User"\]
|
|
39
|
+
|
|
40
|
+
## **🔍 Debug & Inspect**
|
|
41
|
+
|
|
15
42
|
Use the official MCP Inspector to test your server live:
|
|
16
|
-
|
|
17
|
-
npx @modelcontextprotocol/inspector
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
### Environment Variables (Breaking change in 1.0.0)
|
|
22
|
-
|
|
43
|
+
|
|
44
|
+
npx @modelcontextprotocol/inspector \\
|
|
45
|
+
\-e ENDPOINT=\[https://api.example.com/graphql\](https://api.example.com/graphql) \\
|
|
46
|
+
npx @letoribo/mcp-graphql-enhanced \--debug
|
|
47
|
+
|
|
48
|
+
### **Environment Variables (Breaking change in 1.0.0)**
|
|
49
|
+
|
|
50
|
+
**Note:** As of version 1.0.0, command line arguments have been replaced with environment variables.
|
|
23
51
|
|
|
24
52
|
| Environment Variable | Description | Default |
|
|
25
|
-
|
|
26
|
-
|
|
|
27
|
-
|
|
|
28
|
-
|
|
|
29
|
-
|
|
|
30
|
-
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
ENDPOINT=
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
| :---- | :---- | :---- |
|
|
54
|
+
| ENDPOINT | GraphQL endpoint URL | https://mcp-neo4j-discord.vercel.app/api/graphiql |
|
|
55
|
+
| HEADERS | JSON string containing headers for requests | {} |
|
|
56
|
+
| ALLOW\_MUTATIONS | Enable mutation operations (disabled by default) | false |
|
|
57
|
+
| NAME | Name of the MCP server | mcp-graphql-enhanced |
|
|
58
|
+
| SCHEMA | Path to a local GraphQL schema file or URL (optional) | \- |
|
|
59
|
+
| **MCP\_PORT** | **Port for the HTTP/JSON-RPC server.** | 6274 |
|
|
60
|
+
|
|
61
|
+
### **Examples**
|
|
62
|
+
|
|
63
|
+
\# Basic usage
|
|
64
|
+
ENDPOINT=http://localhost:3000/graphql npx @letoribo/mcp-graphql-enhanced
|
|
65
|
+
\# With auth header
|
|
66
|
+
ENDPOINT=\[https://api.example.com/graphql\](https://api.example.com/graphql) \\
|
|
67
|
+
HEADERS='{"Authorization":"Bearer xyz"}' \\
|
|
68
|
+
npx @letoribo/mcp-graphql-enhanced
|
|
69
|
+
\# Enable mutations
|
|
70
|
+
ENDPOINT=http://localhost:3000/graphql \\
|
|
71
|
+
ALLOW\_MUTATIONS=true \\
|
|
72
|
+
npx @letoribo/mcp-graphql-enhanced
|
|
73
|
+
\# Use local schema file
|
|
74
|
+
ENDPOINT=http://localhost:3000/graphql \\
|
|
75
|
+
SCHEMA=./schema.graphql \\
|
|
76
|
+
npx @letoribo/mcp-graphql-enhanced
|
|
77
|
+
\# Change the HTTP port
|
|
78
|
+
MCP\_PORT=8080 npx @letoribo/mcp-graphql-enhanced
|
|
79
|
+
|
|
80
|
+
### **🖥️ Claude Desktop Configuration Examples**
|
|
81
|
+
|
|
49
82
|
You can connect Claude Desktop to your GraphQL API using either the npx package (recommended for simplicity) or the Docker image (ideal for reproducibility and isolation).
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
83
|
+
|
|
84
|
+
### **✅ Option 1: Using npx**
|
|
85
|
+
|
|
86
|
+
{
|
|
87
|
+
"mcpServers": {
|
|
88
|
+
"mcp-graphql-enhanced": {
|
|
89
|
+
"command": "npx",
|
|
90
|
+
"args": \["@letoribo/mcp-graphql-enhanced"\],
|
|
91
|
+
"env": {
|
|
92
|
+
"ENDPOINT": "\[https://your-api.com/graphql\](https://your-api.com/graphql)"
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
62
96
|
}
|
|
63
|
-
|
|
64
|
-
###
|
|
65
|
-
|
|
66
|
-
{
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
97
|
+
|
|
98
|
+
### **🐳 Option 2: Using Docker (auto-pull supported)**
|
|
99
|
+
|
|
100
|
+
{
|
|
101
|
+
"mcpServers": {
|
|
102
|
+
"mcp-graphql-enhanced": {
|
|
103
|
+
"command": "sh",
|
|
104
|
+
"args": \[
|
|
105
|
+
"-c",
|
|
106
|
+
"docker run \--rm \-i \-e ENDPOINT=$ENDPOINT \-e HEADERS=$HEADERS \-e ALLOW\_MUTATIONS=$ALLOW\_MUTATIONS ghcr.io/letoribo/mcp-graphql-enhanced:main"
|
|
107
|
+
\],
|
|
108
|
+
"env": {
|
|
109
|
+
"ENDPOINT": "\[https://your-api.com/graphql\](https://your-api.com/graphql)",
|
|
110
|
+
"HEADERS": "{\\"Authorization\\": \\"Bearer YOUR\_TOKEN\\"}",
|
|
111
|
+
"ALLOW\_MUTATIONS": "false"
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
81
115
|
}
|
|
82
|
-
|
|
83
|
-
###
|
|
116
|
+
|
|
117
|
+
### **🧪 Option 3: Using node with local build (for development)**
|
|
118
|
+
|
|
84
119
|
If you’ve cloned the repo and built the project (npm run build → outputs to dist/):
|
|
85
|
-
|
|
86
|
-
{
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
120
|
+
|
|
121
|
+
{
|
|
122
|
+
"mcpServers": {
|
|
123
|
+
"mcp-graphql-enhanced": {
|
|
124
|
+
"command": "node",
|
|
125
|
+
"args": \["dist/index.js"\],
|
|
126
|
+
"env": {
|
|
127
|
+
"ENDPOINT": "\[https://your-api.com/graphql\](https://your-api.com/graphql)",
|
|
128
|
+
"ALLOW\_MUTATIONS": "true"
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
97
132
|
}
|
|
98
|
-
|
|
99
|
-
## Resources
|
|
100
|
-
|
|
101
|
-
|
|
133
|
+
|
|
134
|
+
## **Resources**
|
|
135
|
+
|
|
136
|
+
* **graphql-schema**: The server exposes the GraphQL schema as a resource that clients can access. This is either the local schema file, a schema file hosted at a URL, or based on an introspection query.
|
|
137
|
+
|
|
138
|
+
## **Available Tools**
|
|
139
|
+
|
|
102
140
|
The server provides two main tools:
|
|
103
|
-
|
|
104
|
-
This
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
141
|
+
|
|
142
|
+
1. introspect-schema: This tool retrieves the GraphQL schema or a filtered subset (via typeNames). Use this first if you don't have access to the schema as a resource.
|
|
143
|
+
This uses either the local schema file, a schema file hosted at a URL, or an introspection query.
|
|
144
|
+
Filtered introspection (typeNames) is only available when using a live GraphQL endpoint (not with SCHEMA file or URL).
|
|
145
|
+
2. **query-graphql**: Execute GraphQL queries against the endpoint. By default, mutations are disabled unless ALLOW\_MUTATIONS is set to true.
|
|
146
|
+
|
|
147
|
+
## **Security Considerations**
|
|
148
|
+
|
|
108
149
|
Mutations are disabled by default to prevent unintended data changes. Always validate HEADERS and SCHEMA inputs in production. Use HTTPS endpoints and short-lived tokens where possible.
|
|
109
|
-
|
|
110
|
-
|
|
150
|
+
|
|
151
|
+
## **Customize for your own server**
|
|
152
|
+
|
|
153
|
+
This is a very generic implementation where it allows for complete introspection and for your users to do whatever (including mutations). If you need a more specific implementation I'd suggest to just create your own MCP and lock down tool calling for clients to only input specific query fields and/or variables. You can use this as a reference.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,3 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
declare const StdioServerTransport: any;
|
|
4
|
-
declare const parse: any;
|
|
5
|
-
declare const z: any;
|
|
6
|
-
declare const checkDeprecatedArguments: any;
|
|
7
|
-
declare const introspectEndpoint: any, introspectLocalSchema: any, introspectSchemaFromUrl: any, introspectTypes: any;
|
|
8
|
-
declare const getVersion: () => any;
|
|
9
|
-
declare const EnvSchema: any;
|
|
10
|
-
declare const env: any;
|
|
11
|
-
declare const server: any;
|
|
12
|
-
interface IntrospectSchemaArgs {
|
|
13
|
-
typeNames?: string[];
|
|
14
|
-
descriptions?: boolean;
|
|
15
|
-
directives?: boolean;
|
|
16
|
-
}
|
|
17
|
-
declare function main(): Promise<void>;
|
|
2
|
+
export {};
|
|
18
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
package/dist/index.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const http = require("node:http");
|
|
3
5
|
const { McpServer } = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
4
6
|
const { StdioServerTransport } = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
5
7
|
const { parse } = require("graphql/language");
|
|
6
8
|
const z = require("zod").default;
|
|
7
|
-
const { checkDeprecatedArguments } = require("./helpers/deprecation.
|
|
8
|
-
const { introspectEndpoint, introspectLocalSchema, introspectSchemaFromUrl, introspectTypes, } = require("./helpers/introspection.
|
|
9
|
-
// Simulate macro import — since "with { type: 'macro' }" is not CommonJS compatible
|
|
9
|
+
const { checkDeprecatedArguments } = require("./helpers/deprecation.ts");
|
|
10
|
+
const { introspectEndpoint, introspectLocalSchema, introspectSchemaFromUrl, introspectTypes, } = require("./helpers/introspection.ts");
|
|
10
11
|
const getVersion = () => {
|
|
11
|
-
// Replace with your actual version or read from package.json
|
|
12
12
|
const pkg = require("../package.json");
|
|
13
13
|
return pkg.version;
|
|
14
14
|
};
|
|
15
|
-
// Check for deprecated command line arguments
|
|
16
15
|
checkDeprecatedArguments();
|
|
17
16
|
const EnvSchema = z.object({
|
|
18
17
|
NAME: z.string().default("mcp-graphql-enhanced"),
|
|
@@ -33,6 +32,7 @@ const EnvSchema = z.object({
|
|
|
33
32
|
}
|
|
34
33
|
}),
|
|
35
34
|
SCHEMA: z.string().optional(),
|
|
35
|
+
MCP_PORT: z.preprocess((val) => (val ? parseInt(val) : 6274), z.number().int().min(1024).max(65535)).default(6274),
|
|
36
36
|
});
|
|
37
37
|
const env = EnvSchema.parse(process.env);
|
|
38
38
|
const server = new McpServer({
|
|
@@ -68,21 +68,14 @@ server.resource("graphql-schema", new URL(env.ENDPOINT).href, async (uri) => {
|
|
|
68
68
|
throw new Error(`Failed to get GraphQL schema: ${error}`);
|
|
69
69
|
}
|
|
70
70
|
});
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
endpoint: z.string().optional(),
|
|
74
|
-
headers: z.string().optional(),
|
|
75
|
-
descriptions: z.boolean().optional().default(true),
|
|
76
|
-
directives: z.boolean().optional().default(true),
|
|
77
|
-
}, async ({ typeNames, descriptions = true, directives = true }) => {
|
|
71
|
+
const toolHandlers = new Map();
|
|
72
|
+
const introspectSchemaHandler = async ({ typeNames, descriptions = true, directives = true }) => {
|
|
78
73
|
try {
|
|
79
74
|
if (typeNames && typeNames.length > 0) {
|
|
80
|
-
// ✅ Use your existing introspectTypes helper
|
|
81
75
|
const filtered = await introspectTypes(env.ENDPOINT, env.HEADERS, typeNames);
|
|
82
76
|
return { content: [{ type: "text", text: filtered }] };
|
|
83
77
|
}
|
|
84
78
|
else {
|
|
85
|
-
// Fallback to full schema
|
|
86
79
|
let schema;
|
|
87
80
|
if (env.SCHEMA) {
|
|
88
81
|
if (env.SCHEMA.startsWith("http://") || env.SCHEMA.startsWith("https://")) {
|
|
@@ -104,15 +97,14 @@ server.tool("introspect-schema", "Introspect the GraphQL schema. Optionally filt
|
|
|
104
97
|
content: [{ type: "text", text: `Introspection failed: ${error}` }],
|
|
105
98
|
};
|
|
106
99
|
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}, async ({ query, variables, headers }) => {
|
|
100
|
+
};
|
|
101
|
+
toolHandlers.set("introspect-schema", introspectSchemaHandler);
|
|
102
|
+
server.tool("introspect-schema", "Introspect the GraphQL schema. Optionally filter to specific types.", {
|
|
103
|
+
typeNames: z.array(z.string()).optional().describe("e.g., [\"Query\", \"User\"]"),
|
|
104
|
+
descriptions: z.boolean().optional().default(true),
|
|
105
|
+
directives: z.boolean().optional().default(true),
|
|
106
|
+
}, introspectSchemaHandler);
|
|
107
|
+
const queryGraphqlHandler = async ({ query, variables, headers }) => {
|
|
116
108
|
try {
|
|
117
109
|
const parsedQuery = parse(query);
|
|
118
110
|
const isMutation = parsedQuery.definitions.some((def) => def.kind === "OperationDefinition" && def.operation === "mutation");
|
|
@@ -148,7 +140,6 @@ server.tool("query-graphql", "Query a GraphQL endpoint with the given query and
|
|
|
148
140
|
...env.HEADERS,
|
|
149
141
|
...toolHeaders,
|
|
150
142
|
};
|
|
151
|
-
// Parse variables if it's a string
|
|
152
143
|
let parsedVariables = null;
|
|
153
144
|
if (variables) {
|
|
154
145
|
if (typeof variables === 'string') {
|
|
@@ -179,7 +170,6 @@ server.tool("query-graphql", "Query a GraphQL endpoint with the given query and
|
|
|
179
170
|
};
|
|
180
171
|
}
|
|
181
172
|
const rawData = await response.json();
|
|
182
|
-
// Type assertion for quick dev (replace with zod validation later)
|
|
183
173
|
const data = rawData;
|
|
184
174
|
if (data.errors && data.errors.length > 0) {
|
|
185
175
|
return {
|
|
@@ -212,11 +202,114 @@ server.tool("query-graphql", "Query a GraphQL endpoint with the given query and
|
|
|
212
202
|
],
|
|
213
203
|
};
|
|
214
204
|
}
|
|
215
|
-
}
|
|
205
|
+
};
|
|
206
|
+
toolHandlers.set("query-graphql", queryGraphqlHandler);
|
|
207
|
+
server.tool("query-graphql", "Query a GraphQL endpoint with the given query and variables. Optionally pass headers (e.g., for Authorization).", {
|
|
208
|
+
query: z.string(),
|
|
209
|
+
variables: z.string().optional(),
|
|
210
|
+
headers: z
|
|
211
|
+
.string()
|
|
212
|
+
.optional()
|
|
213
|
+
.describe("Optional JSON string of headers to include, e.g., {\"Authorization\": \"Bearer ...\"}"),
|
|
214
|
+
}, queryGraphqlHandler);
|
|
215
|
+
function readBody(req) {
|
|
216
|
+
return new Promise((resolve, reject) => {
|
|
217
|
+
let body = '';
|
|
218
|
+
req.on('data', (chunk) => {
|
|
219
|
+
body += chunk.toString();
|
|
220
|
+
});
|
|
221
|
+
req.on('end', () => {
|
|
222
|
+
resolve(body);
|
|
223
|
+
});
|
|
224
|
+
req.on('error', reject);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
async function handleHttpRequest(req, res) {
|
|
228
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
229
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
230
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
231
|
+
if (req.method === 'OPTIONS') {
|
|
232
|
+
res.writeHead(204);
|
|
233
|
+
res.end();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
237
|
+
if (url.pathname === '/health' && req.method === 'GET') {
|
|
238
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
239
|
+
res.end(JSON.stringify({ status: 'ok', server: env.NAME }));
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (url.pathname === '/mcp' && req.method === 'POST') {
|
|
243
|
+
let rawBody;
|
|
244
|
+
let request;
|
|
245
|
+
try {
|
|
246
|
+
rawBody = await readBody(req);
|
|
247
|
+
request = JSON.parse(rawBody);
|
|
248
|
+
}
|
|
249
|
+
catch (e) {
|
|
250
|
+
console.error("HTTP MCP Parse Error:", e);
|
|
251
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
252
|
+
res.end(JSON.stringify({
|
|
253
|
+
jsonrpc: '2.0',
|
|
254
|
+
id: null,
|
|
255
|
+
error: { code: -32700, message: 'Parse Error: Invalid JSON received in request body.' }
|
|
256
|
+
}));
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
try {
|
|
260
|
+
const { method, params, id } = request;
|
|
261
|
+
if (!method || typeof id === 'undefined') {
|
|
262
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
263
|
+
res.end(JSON.stringify({
|
|
264
|
+
jsonrpc: '2.0',
|
|
265
|
+
id: id || null,
|
|
266
|
+
error: { code: -32600, message: 'Invalid JSON-RPC Request structure (missing method or id).' }
|
|
267
|
+
}));
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const handler = toolHandlers.get(method);
|
|
271
|
+
if (!handler) {
|
|
272
|
+
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
273
|
+
res.end(JSON.stringify({
|
|
274
|
+
jsonrpc: '2.0',
|
|
275
|
+
id: id,
|
|
276
|
+
error: { code: -32601, message: `Method not found: ${method}` }
|
|
277
|
+
}));
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const result = await handler(params);
|
|
281
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
282
|
+
res.end(JSON.stringify({
|
|
283
|
+
jsonrpc: '2.0',
|
|
284
|
+
id: id,
|
|
285
|
+
result: result
|
|
286
|
+
}));
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
console.error("HTTP MCP Execution Error:", error);
|
|
290
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
291
|
+
res.end(JSON.stringify({
|
|
292
|
+
jsonrpc: '2.0',
|
|
293
|
+
id: request?.id || null,
|
|
294
|
+
error: { code: -32603, message: 'Internal server error during tool execution.' }
|
|
295
|
+
}));
|
|
296
|
+
}
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
300
|
+
res.end('Not Found. Use POST /mcp for JSON-RPC or GET /health.');
|
|
301
|
+
}
|
|
302
|
+
function startHttpTransport() {
|
|
303
|
+
const server = http.createServer(handleHttpRequest);
|
|
304
|
+
server.listen(env.MCP_PORT, () => {
|
|
305
|
+
console.error(`[HTTP] Started server on http://localhost:${env.MCP_PORT}. Listening for POST /mcp requests.`);
|
|
306
|
+
});
|
|
307
|
+
}
|
|
216
308
|
async function main() {
|
|
217
|
-
const
|
|
218
|
-
await server.connect(
|
|
219
|
-
|
|
309
|
+
const stdioTransport = new StdioServerTransport();
|
|
310
|
+
await server.connect(stdioTransport);
|
|
311
|
+
startHttpTransport();
|
|
312
|
+
console.error(`[STDIO] Started graphql mcp server ${env.NAME} for endpoint: ${env.ENDPOINT}`);
|
|
220
313
|
}
|
|
221
314
|
main().catch((error) => {
|
|
222
315
|
console.error(`Fatal error in main(): ${error}`);
|
package/package.json
CHANGED