-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
186 lines (152 loc) · 5.72 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
// Based on implementation from https://github.com/supercorp-ai/supergateway
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import type { JSONRPCMessage, JSONRPCRequest } from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
import * as pkg from "./package.json";
import { Option, program } from "commander";
import 'dotenv/config';
// defaults
const AUTH_HEADER_NAME = "Authorization";
const VERSION = pkg.version;
// CLI
const opts = program
.name(pkg.name)
.version(VERSION)
.description(pkg.description)
.showHelpAfterError()
.addOption(new Option("--sse-url <string>", "Server-Sent Events (SSE) url").env("SSE_URL"))
.addOption(new Option("--access-token <string>", "JWT Access Token from https://thegraph.market").env("ACCESS_TOKEN"))
.addOption(new Option("-v, --verbose <boolean>", "Enable verbose logging").choices(["true", "false"]).env("VERBOSE").default(false))
.parse()
.opts();
const ACCESS_TOKEN = opts.accessToken;
const SSE_URL = opts.sseUrl;
const AUTH_HEADER_VALUE = `Bearer ${ACCESS_TOKEN}`;
// CLI or .env validation
if (!ACCESS_TOKEN) {
console.error("Error: Missing required ACCESS_TOKEN .env variable or --access-token option");
process.exit(1);
} else {
const jwtSchema = z.string().regex(/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/);
const result = jwtSchema.safeParse(ACCESS_TOKEN);
if (!result.success) {
console.error("Error: Invalid ACCESS_TOKEN .env variable or --access-token option");
process.exit(1);
}
}
if (!SSE_URL) {
console.error("Error: Missing required SSE_URL .env variable or --sse-url option");
process.exit(1);
} else {
const result = z.string().url().safeParse(SSE_URL);
if (!result.success) {
console.error("Error: Invalid SSE_URL .env variable or --sse-url option");
process.exit(1);
}
}
// Using console.error as logger since stdout is used for MCP communication
// See https://modelcontextprotocol.io/docs/tools/debugging#server-side-logging
const logger = { info: opts.verbose ? console.error : () => {} };
logger.info(`Connecting to remote SSE at ${SSE_URL}`);
process.on('SIGINT', () => {
logger.info('Caught SIGINT. Exiting...');
process.exit(0);
});
process.on('SIGTERM', () => {
logger.info('Caught SIGTERM. Exiting...');
process.exit(0);
});
process.on('SIGHUP', () => {
logger.info('Caught SIGHUP. Exiting...');
process.exit(0);
});
process.stdin.on('close', () => {
logger.info('stdin closed. Exiting...');
process.exit(0);
});
const fetchWithAuth = (url: string | URL, init?: RequestInit) => {
const headers = new Headers(init?.headers as Bun.HeadersInit);
headers.set(AUTH_HEADER_NAME, AUTH_HEADER_VALUE);
return fetch(url.toString(), { ...init, headers });
};
const sseTransport = new SSEClientTransport(new URL(SSE_URL), {
// Adds auth header to initial connect (e.g. `/sse`)
eventSourceInit: {
fetch: fetchWithAuth,
},
// Adds auth header to all subsequent messages (e.g. `/messages`)
requestInit: {
headers: { [AUTH_HEADER_NAME]: AUTH_HEADER_VALUE },
}
});
const sseClient = new Client(
{ name: 'MCP SSE Client', version: VERSION },
{ capabilities: {} }
);
sseTransport.onerror = err => {
logger.info('SSE error:', err);
};
sseTransport.onclose = () => {
logger.info('SSE connection closed');
process.exit(1);
};
await sseClient.connect(sseTransport);
logger.info('SSE successfully connected !');
const stdioServer = new Server(
sseClient.getServerVersion() ?? { name: 'MCP Stdio Server', version: VERSION },
{ capabilities: sseClient.getServerCapabilities() }
);
const stdioTransport = new StdioServerTransport();
await stdioServer.connect(stdioTransport);
const wrapResponse = (req: JSONRPCRequest, payload: object) => ({
jsonrpc: req.jsonrpc || '2.0',
id: req.id,
...payload,
});
stdioServer.transport!.onmessage = async (message: JSONRPCMessage) => {
const isRequest = 'method' in message && 'id' in message;
if (isRequest) {
logger.info('Stdio → SSE:', message);
const req = message as JSONRPCRequest;
let result;
try {
result = await sseClient.request(req, z.any());
} catch (err) {
logger.info('Request error:', err);
const errorCode =
err && typeof err === 'object' && 'code' in err
? (err as any).code
: -32000;
let errorMsg =
err && typeof err === 'object' && 'message' in err
? (err as any).message
: 'Internal error';
const prefix = `MCP error ${errorCode}:`;
if (errorMsg.startsWith(prefix))
errorMsg = errorMsg.slice(prefix.length).trim();
const errorResp = wrapResponse(req, {
error: {
code: errorCode,
message: errorMsg,
},
});
process.stdout.write(JSON.stringify(errorResp) + '\n');
return;
}
const response = wrapResponse(
req,
result.hasOwnProperty('error')
? { error: { ...result.error } }
: { result: { ...result } }
);
logger.info('Response:', response);
process.stdout.write(JSON.stringify(response) + '\n');
} else {
logger.info('SSE → Stdio:', message);
process.stdout.write(JSON.stringify(message) + '\n');
}
};