forked from nicobailon/pi-mcp-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-manager.ts
More file actions
389 lines (334 loc) · 12.5 KB
/
server-manager.ts
File metadata and controls
389 lines (334 loc) · 12.5 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
import { UnauthorizedError } from "@modelcontextprotocol/sdk/client/auth.js";
import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
import type {
McpTool,
McpResource,
ServerDefinition,
ServerStreamResultPatchNotification,
Transport,
} from "./types.js";
import { serverStreamResultPatchNotificationSchema } from "./types.js";
import { resolveNpxBinary } from "./npx-resolver.js";
import { logger } from "./logger.js";
import { McpOAuthProvider } from "./mcp-oauth-provider.js";
import { supportsOAuth } from "./mcp-auth-flow.js";
import { registerSamplingHandler, type ServerSamplingConfig } from "./sampling-handler.js";
interface ServerConnection {
client: Client;
transport: Transport;
definition: ServerDefinition;
tools: McpTool[];
resources: McpResource[];
lastUsedAt: number;
inFlight: number;
status: "connected" | "closed" | "needs-auth";
}
type UiStreamListener = (serverName: string, notification: ServerStreamResultPatchNotification["params"]) => void;
export class McpServerManager {
private connections = new Map<string, ServerConnection>();
private connectPromises = new Map<string, Promise<ServerConnection>>();
private uiStreamListeners = new Map<string, UiStreamListener>();
private samplingConfig: ServerSamplingConfig | undefined;
setSamplingConfig(config: ServerSamplingConfig | undefined): void {
this.samplingConfig = config;
}
async connect(name: string, definition: ServerDefinition): Promise<ServerConnection> {
// Dedupe concurrent connection attempts
if (this.connectPromises.has(name)) {
return this.connectPromises.get(name)!;
}
// Reuse existing connection if healthy
const existing = this.connections.get(name);
if (existing?.status === "connected") {
existing.lastUsedAt = Date.now();
return existing;
}
const promise = this.createConnection(name, definition);
this.connectPromises.set(name, promise);
try {
const connection = await promise;
this.connections.set(name, connection);
return connection;
} finally {
this.connectPromises.delete(name);
}
}
private async createConnection(
name: string,
definition: ServerDefinition
): Promise<ServerConnection> {
const client = this.createClient(name);
let transport: Transport;
if (definition.command) {
let command = definition.command;
let args = definition.args ?? [];
if (command === "npx" || command === "npm") {
const resolved = await resolveNpxBinary(command, args);
if (resolved) {
command = resolved.isJs ? "node" : resolved.binPath;
args = resolved.isJs ? [resolved.binPath, ...resolved.extraArgs] : resolved.extraArgs;
logger.debug(`${name} resolved to ${resolved.binPath} (skipping npm parent)`);
}
}
transport = new StdioClientTransport({
command,
args,
env: resolveEnv(definition.env),
cwd: definition.cwd,
stderr: definition.debug ? "inherit" : "ignore",
});
} else if (definition.url) {
// HTTP transport with fallback
transport = await this.createHttpTransport(definition, name);
} else {
throw new Error(`Server ${name} has no command or url`);
}
try {
await client.connect(transport);
this.attachAdapterNotificationHandlers(name, client);
// Discover tools and resources
const [tools, resources] = await Promise.all([
this.fetchAllTools(client),
this.fetchAllResources(client),
]);
return {
client,
transport,
definition,
tools,
resources,
lastUsedAt: Date.now(),
inFlight: 0,
status: "connected",
};
} catch (error) {
// Check for UnauthorizedError - server requires OAuth
if (error instanceof UnauthorizedError && supportsOAuth(definition)) {
// Clean up both client and transport before reporting needs-auth.
await client.close().catch(() => {});
await transport.close().catch(() => {});
return {
client,
transport,
definition,
tools: [],
resources: [],
lastUsedAt: Date.now(),
inFlight: 0,
status: "needs-auth",
};
}
// Clean up both client and transport on any error
await client.close().catch(() => {});
await transport.close().catch(() => {});
throw error;
}
}
private createClient(serverName: string): Client {
const client = new Client(
{ name: `pi-mcp-${serverName}`, version: "1.0.0" },
this.samplingConfig ? { capabilities: { sampling: {} } } : undefined,
);
if (this.samplingConfig) {
registerSamplingHandler(client, { ...this.samplingConfig, serverName });
}
return client;
}
private async createHttpTransport(
definition: ServerDefinition,
serverName: string
): Promise<Transport> {
const url = new URL(definition.url!);
// Build headers first (including any bearer token)
const headers = resolveHeaders(definition.headers) ?? {};
// For bearer auth, add the token to headers BEFORE creating requestInit
if (definition.auth === "bearer") {
const token = definition.bearerToken
?? (definition.bearerTokenEnv ? process.env[definition.bearerTokenEnv] : undefined);
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
}
// Create request init with headers (Authorization now included for bearer auth)
const requestInit = Object.keys(headers).length > 0 ? { headers } : undefined;
// For OAuth servers, create an auth provider
let authProvider: McpOAuthProvider | undefined;
if (supportsOAuth(definition)) {
// Extract OAuth config (handles both object and false cases)
const oauthConfig = definition.oauth === false ? {} : {
grantType: definition.oauth?.grantType,
clientId: definition.oauth?.clientId,
clientSecret: definition.oauth?.clientSecret,
scope: definition.oauth?.scope,
};
authProvider = new McpOAuthProvider(
serverName,
definition.url!,
oauthConfig,
{
onRedirect: async (_authUrl) => {
// URL is captured by startAuth, no need to log
},
}
);
}
// Try StreamableHTTP first (modern MCP servers)
const streamableTransport = new StreamableHTTPClientTransport(url, {
requestInit,
authProvider,
});
try {
// Create a test client to verify the transport works
const testClient = new Client({ name: "pi-mcp-probe", version: "2.1.2" });
await testClient.connect(streamableTransport);
await testClient.close().catch(() => {});
// Close probe transport before creating fresh one
await streamableTransport.close().catch(() => {});
// StreamableHTTP works - create fresh transport for actual use
return new StreamableHTTPClientTransport(url, { requestInit, authProvider });
} catch (error) {
// StreamableHTTP failed, close and try SSE fallback
await streamableTransport.close().catch(() => {});
// If this was an UnauthorizedError, don't try SSE - the server needs auth
if (error instanceof UnauthorizedError) {
throw error;
}
// SSE is the legacy transport
return new SSEClientTransport(url, { requestInit, authProvider });
}
}
private async fetchAllTools(client: Client): Promise<McpTool[]> {
const allTools: McpTool[] = [];
let cursor: string | undefined;
do {
const result = await client.listTools(cursor ? { cursor } : undefined);
allTools.push(...(result.tools ?? []));
cursor = result.nextCursor;
} while (cursor);
return allTools;
}
private async fetchAllResources(client: Client): Promise<McpResource[]> {
try {
const allResources: McpResource[] = [];
let cursor: string | undefined;
do {
const result = await client.listResources(cursor ? { cursor } : undefined);
allResources.push(...(result.resources ?? []));
cursor = result.nextCursor;
} while (cursor);
return allResources;
} catch {
// Server may not support resources
return [];
}
}
private attachAdapterNotificationHandlers(serverName: string, client: Client): void {
client.setNotificationHandler(serverStreamResultPatchNotificationSchema, (notification) => {
const listener = this.uiStreamListeners.get(notification.params.streamToken);
if (!listener) return;
listener(serverName, notification.params);
});
}
registerUiStreamListener(streamToken: string, listener: UiStreamListener): void {
this.uiStreamListeners.set(streamToken, listener);
}
removeUiStreamListener(streamToken: string): void {
this.uiStreamListeners.delete(streamToken);
}
async readResource(name: string, uri: string): Promise<ReadResourceResult> {
const connection = this.connections.get(name);
if (!connection || connection.status !== "connected") {
throw new Error(`Server "${name}" is not connected`);
}
try {
this.touch(name);
this.incrementInFlight(name);
return await connection.client.readResource({ uri });
} finally {
this.decrementInFlight(name);
this.touch(name);
}
}
async close(name: string): Promise<void> {
const connection = this.connections.get(name);
if (!connection) return;
// Delete from map BEFORE async cleanup to prevent a race where a
// concurrent connect() creates a new connection that our deferred
// delete() would then remove, orphaning the new server process.
connection.status = "closed";
this.connections.delete(name);
await connection.client.close().catch(() => {});
await connection.transport.close().catch(() => {});
}
async closeAll(): Promise<void> {
const names = [...this.connections.keys()];
await Promise.all(names.map(name => this.close(name)));
}
getConnection(name: string): ServerConnection | undefined {
return this.connections.get(name);
}
getAllConnections(): Map<string, ServerConnection> {
return new Map(this.connections);
}
touch(name: string): void {
const connection = this.connections.get(name);
if (connection) {
connection.lastUsedAt = Date.now();
}
}
incrementInFlight(name: string): void {
const connection = this.connections.get(name);
if (connection) {
connection.inFlight = (connection.inFlight ?? 0) + 1;
}
}
decrementInFlight(name: string): void {
const connection = this.connections.get(name);
if (connection && connection.inFlight) {
connection.inFlight--;
}
}
isIdle(name: string, timeoutMs: number): boolean {
const connection = this.connections.get(name);
if (!connection || connection.status !== "connected") return false;
if (connection.inFlight > 0) return false;
return (Date.now() - connection.lastUsedAt) > timeoutMs;
}
}
/**
* Resolve environment variables with interpolation.
*/
function resolveEnv(env?: Record<string, string>): Record<string, string> {
// Copy process.env, filtering out undefined values
const resolved: Record<string, string> = {};
for (const [key, value] of Object.entries(process.env)) {
if (value !== undefined) {
resolved[key] = value;
}
}
if (!env) return resolved;
for (const [key, value] of Object.entries(env)) {
// Support ${VAR} and $env:VAR interpolation
resolved[key] = value
.replace(/\$\{(\w+)\}/g, (_, name) => process.env[name] ?? "")
.replace(/\$env:(\w+)/g, (_, name) => process.env[name] ?? "");
}
return resolved;
}
/**
* Resolve headers with environment variable interpolation.
*/
function resolveHeaders(headers?: Record<string, string>): Record<string, string> | undefined {
if (!headers) return undefined;
const resolved: Record<string, string> = {};
for (const [key, value] of Object.entries(headers)) {
resolved[key] = value
.replace(/\$\{(\w+)\}/g, (_, name) => process.env[name] ?? "")
.replace(/\$env:(\w+)/g, (_, name) => process.env[name] ?? "");
}
return resolved;
}