Skip to content

Commit 0690b1f

Browse files
committed
Refactoring to move dependencies upstream
1 parent dc6e270 commit 0690b1f

File tree

4 files changed

+58
-40
lines changed

4 files changed

+58
-40
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
7+
import type * as vscode from 'vscode';
8+
9+
/**
10+
* Options for registering an in-proc MCP HTTP provider
11+
*/
12+
export interface McpProviderOptions {
13+
/**
14+
* The ID for this MCP provider. Must match what is in package.json's contributions.mcpServerDefinitionProviders section
15+
*/
16+
readonly id: string;
17+
18+
/**
19+
* A human-readable label for the server
20+
*/
21+
readonly serverLabel: string;
22+
23+
/**
24+
* The version of the MCP server. VSCode does use this as a hint on whether it should refresh the tool list
25+
*/
26+
readonly serverVersion: string;
27+
28+
/**
29+
* Called to register tools on the MCP server after it is created
30+
* @param server The server to register tools on
31+
*/
32+
registerTools: (server: McpServer) => void | Promise<void>;
33+
34+
/**
35+
* Optional event that fires when the set of available MCP server definitions changes
36+
*/
37+
onDidChange?: vscode.Event<void>;
38+
}

packages/vscode-inproc-mcp/src/vscode/inProcHttpServer.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import type { DisposableLike } from '@microsoft/vscode-processutils';
7-
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
87
import type { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
98
import * as crypto from 'crypto';
109
import type * as express from 'express';
@@ -13,17 +12,17 @@ import * as os from 'os';
1312
import * as path from 'path';
1413
import * as vscode from 'vscode';
1514
import { Lazy } from '../utils/Lazy';
15+
import type { McpProviderOptions } from './McpProviderOptions';
1616

1717
const transports: Record<string, StreamableHTTPServerTransport> = {};
1818

1919
/**
2020
* Starts a new MCP HTTP server instance on a random named pipe (Windows) or Unix socket (Unix).
21-
* @param getNewMcpServer Function that returns a new MCP server instance. A new server must be created
22-
* for each MCP session, so this function should not return the same instance each time.
21+
* @param mcpOptions Options for the MCP server
2322
* @returns An object containing the disposable to stop and clean up the server, the server URI, and headers
2423
* that should be attached to all requests
2524
*/
26-
export async function startInProcHttpServer(getNewMcpServer: () => McpServer | Promise<McpServer>): Promise<{ disposable: DisposableLike, serverUri: vscode.Uri, headers: Record<string, string> }> {
25+
export async function startInProcHttpServer(mcpOptions: McpProviderOptions): Promise<{ disposable: DisposableLike, serverUri: vscode.Uri, headers: Record<string, string> }> {
2726
let socketPath: string | undefined;
2827

2928
try {
@@ -37,7 +36,7 @@ export async function startInProcHttpServer(getNewMcpServer: () => McpServer | P
3736
app.use(express.default.json());
3837
app.use((req, res, next) => authMiddleware(nonce, req, res, next));
3938

40-
app.post('/mcp', (req, res) => handlePost(getNewMcpServer, req, res));
39+
app.post('/mcp', (req, res) => handlePost(mcpOptions, req, res));
4140
app.get('/mcp', handleGetDelete);
4241
app.delete('/mcp', handleGetDelete);
4342

@@ -86,7 +85,7 @@ function authMiddleware(nonce: string, req: express.Request, res: express.Respon
8685
next();
8786
}
8887

89-
async function handlePost(getNewMcpServer: () => McpServer | Promise<McpServer>, req: express.Request, res: express.Response): Promise<void> {
88+
async function handlePost(mcpOptions: McpProviderOptions, req: express.Request, res: express.Response): Promise<void> {
9089
const sessionId = req.headers['mcp-session-id'] as string | undefined;
9190

9291
const isInitializeRequest = await isInitializeRequestLazy.value;
@@ -112,7 +111,17 @@ async function handlePost(getNewMcpServer: () => McpServer | Promise<McpServer>,
112111
allowedHosts: ['localhost'],
113112
});
114113

115-
const server = await Promise.resolve(getNewMcpServer());
114+
const { McpServer } = await mcpServerLazy.value;
115+
const server = new McpServer(
116+
{
117+
name: mcpOptions.id,
118+
title: mcpOptions.serverLabel,
119+
version: mcpOptions.serverVersion,
120+
}
121+
);
122+
123+
await Promise.resolve(mcpOptions.registerTools(server));
124+
116125
await server.connect(transport);
117126
} else {
118127
// Invalid request
@@ -182,6 +191,7 @@ function tryCleanupSocket(socketPath: string | undefined): void {
182191
// Lazily load some modules that are only needed when an MCP server is actually started
183192
const expressLazy = new Lazy(async () => await import('express'));
184193
const streamableHttpLazy = new Lazy(async () => await import('@modelcontextprotocol/sdk/server/streamableHttp.js'));
194+
const mcpServerLazy = new Lazy(async () => await import('@modelcontextprotocol/sdk/server/mcp.js'));
185195
const isInitializeRequestLazy = new Lazy(async () => {
186196
const { isInitializeRequest } = await import('@modelcontextprotocol/sdk/types.js');
187197
return isInitializeRequest;

packages/vscode-inproc-mcp/src/vscode/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
export * from './inProcHttpServer';
7+
export * from './McpProviderOptions';
78
export * from './McpToolWithTelemetry';
89
export * from './registerMcpHttpProvider';
910
export * from './registerMcpToolWithTelemetry';

packages/vscode-inproc-mcp/src/vscode/registerMcpHttpProvider.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,9 @@
33
* Licensed under the MIT License. See LICENSE in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
76
import * as vscode from 'vscode';
87
import { startInProcHttpServer } from './inProcHttpServer';
9-
10-
/**
11-
* Options for registering an in-proc MCP HTTP provider
12-
*/
13-
export type McpProviderOptions = {
14-
/**
15-
* The ID for this MCP provider. Must match what is in package.json
16-
*/
17-
id: string;
18-
19-
/**
20-
* A human-readable label for the server
21-
*/
22-
serverLabel: string;
23-
24-
/**
25-
* The version of the MCP server. VSCode does use this as a hint on whether it should refresh the tool list
26-
*/
27-
serverVersion?: string;
28-
29-
/**
30-
* Function that returns a new MCP server instance. A new server must be created
31-
* for each MCP session, so this function should not return the same instance each time.
32-
*/
33-
getNewMcpServer: () => McpServer | Promise<McpServer>;
34-
35-
/**
36-
* Optional event that fires when the set of available MCP server definitions changes
37-
*/
38-
onDidChange?: vscode.Event<void>;
39-
};
8+
import type { McpProviderOptions } from './McpProviderOptions';
409

4110
/**
4211
* Registers an in-proc MCP HTTP server provider
@@ -57,7 +26,7 @@ export function registerMcpHttpProvider(context: vscode.ExtensionContext, option
5726
];
5827
},
5928
async resolveMcpServerDefinition(server: vscode.McpHttpServerDefinition, token: vscode.CancellationToken): Promise<vscode.McpServerDefinition> {
60-
const { disposable, serverUri, headers } = await startInProcHttpServer(options.getNewMcpServer);
29+
const { disposable, serverUri, headers } = await startInProcHttpServer(options);
6130
context.subscriptions.push(disposable);
6231
server.uri = serverUri;
6332
server.headers = headers;

0 commit comments

Comments
 (0)