-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add support for the new vscode.lm.registerMcpServerDefinitionProvider API #15755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add support for the new vscode.lm.registerMcpServerDefinitionProvider…
… API
- Loading branch information
commit 0ac8ce8ac5ec7f7b1dc53662fb21a961ecb0ab62
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // ***************************************************************************** | ||
| // Copyright (C) 2025 EclipseSource. | ||
| // | ||
| // This program and the accompanying materials are made available under the | ||
| // terms of the Eclipse Public License v. 2.0 which is available at | ||
| // http://www.eclipse.org/legal/epl-2.0. | ||
| // | ||
| // This Source Code may also be made available under the following Secondary | ||
| // Licenses when the conditions for such availability set forth in the Eclipse | ||
| // Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
| // with the GNU Classpath Exception which is available at | ||
| // https://www.gnu.org/software/classpath/license.html. | ||
| // | ||
| // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
| // ***************************************************************************** | ||
|
|
||
| import { UriComponents } from './uri-components'; | ||
|
|
||
| /** | ||
| * Protocol interfaces for MCP server definition providers. | ||
| */ | ||
|
|
||
| export interface McpStdioServerDefinitionDto { | ||
| /** | ||
| * The human-readable name of the server. | ||
| */ | ||
| readonly label: string; | ||
|
|
||
| /** | ||
| * The working directory used to start the server. | ||
| */ | ||
| cwd?: UriComponents; | ||
|
|
||
| /** | ||
| * The command used to start the server. Node.js-based servers may use | ||
| * `process.execPath` to use the editor's version of Node.js to run the script. | ||
| */ | ||
| command: string; | ||
|
|
||
| /** | ||
| * Additional command-line arguments passed to the server. | ||
| */ | ||
| args?: string[]; | ||
|
|
||
| /** | ||
| * Optional additional environment information for the server. Variables | ||
| * in this environment will overwrite or remove (if null) the default | ||
| * environment variables of the editor's extension host. | ||
| */ | ||
| env?: Record<string, string | number | null>; | ||
|
|
||
| /** | ||
| * Optional version identification for the server. If this changes, the | ||
| * editor will indicate that tools have changed and prompt to refresh them. | ||
| */ | ||
| version?: string; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * McpHttpServerDefinition represents an MCP server available using the | ||
| * Streamable HTTP transport. | ||
| */ | ||
| export interface McpHttpServerDefinitionDto { | ||
| /** | ||
| * The human-readable name of the server. | ||
| */ | ||
| readonly label: string; | ||
|
|
||
| /** | ||
| * The URI of the server. The editor will make a POST request to this URI | ||
| * to begin each session. | ||
| */ | ||
| uri: UriComponents; | ||
|
|
||
| /** | ||
| * Optional additional heads included with each request to the server. | ||
| */ | ||
| headers?: Record<string, string>; | ||
|
|
||
| /** | ||
| * Optional version identification for the server. If this changes, the | ||
| * editor will indicate that tools have changed and prompt to refresh them. | ||
| */ | ||
| version?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Definitions that describe different types of Model Context Protocol servers, | ||
| * which can be returned from the {@link McpServerDefinitionProvider}. | ||
| */ | ||
| export type McpServerDefinitionDto = McpStdioServerDefinitionDto | McpHttpServerDefinitionDto; | ||
| export const isMcpHttpServerDefinitionDto = (definition: McpServerDefinitionDto): definition is McpHttpServerDefinitionDto => 'uri' in definition; | ||
| /** | ||
| * Main side of the MCP server definition registry. | ||
| */ | ||
| export interface McpServerDefinitionRegistryMain { | ||
| /** | ||
| * Register an MCP server definition provider. | ||
| */ | ||
| $registerMcpServerDefinitionProvider(handle: number, name: string): void; | ||
|
|
||
| /** | ||
| * Unregister an MCP server definition provider. | ||
| */ | ||
| $unregisterMcpServerDefinitionProvider(handle: number): void; | ||
|
|
||
| /** | ||
| * Notify that server definitions have changed. | ||
| */ | ||
| $onDidChangeMcpServerDefinitions(handle: number): void; | ||
|
|
||
| /** | ||
| * Get server definitions from a provider. | ||
| */ | ||
| $getServerDefinitions(handle: number): Promise<McpServerDefinitionDto[]>; | ||
|
|
||
| /** | ||
| * Resolve a server definition. | ||
| */ | ||
| $resolveServerDefinition(handle: number, server: McpServerDefinitionDto): Promise<McpServerDefinitionDto | undefined>; | ||
| } | ||
|
|
||
| /** | ||
| * Extension side of the MCP server definition registry. | ||
| */ | ||
| export interface McpServerDefinitionRegistryExt { | ||
| /** | ||
| * Request server definitions from a provider. | ||
| */ | ||
| $provideServerDefinitions(handle: number): Promise<McpServerDefinitionDto[]>; | ||
|
|
||
| /** | ||
| * Resolve a server definition from a provider. | ||
| */ | ||
| $resolveServerDefinition(handle: number, server: McpServerDefinitionDto): Promise<McpServerDefinitionDto | undefined>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // ***************************************************************************** | ||
| // Copyright (C) 2025 EclipseSource | ||
| // | ||
| // This program and the accompanying materials are made available under the | ||
| // terms of the Eclipse Public License v. 2.0 which is available at | ||
| // http://www.eclipse.org/legal/epl-2.0. | ||
| // | ||
| // This Source Code may also be made available under the following Secondary | ||
| // Licenses when the conditions for such availability set forth in the Eclipse | ||
| // Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
| // with the GNU Classpath Exception which is available at | ||
| // https://www.gnu.org/software/classpath/license.html. | ||
| // | ||
| // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
| // ***************************************************************************** | ||
|
|
||
| import { interfaces } from '@theia/core/shared/inversify'; | ||
| import { RPCProtocol } from '../../common/rpc-protocol'; | ||
| import { | ||
| McpServerDefinitionRegistryMain, | ||
| McpServerDefinitionRegistryExt, | ||
| McpServerDefinitionDto, | ||
| isMcpHttpServerDefinitionDto, | ||
| } from '../../common/lm-protocol'; | ||
| import { MAIN_RPC_CONTEXT } from '../../common/plugin-api-rpc'; | ||
| import { MCPServerManager, MCPServerDescription } from '@theia/ai-mcp/lib/common'; | ||
|
|
||
| export class McpServerDefinitionRegistryMainImpl implements McpServerDefinitionRegistryMain { | ||
| private readonly proxy: McpServerDefinitionRegistryExt; | ||
| private readonly providers = new Map<number, string>(); | ||
| private readonly mcpServerManager: MCPServerManager | undefined; | ||
|
|
||
| constructor( | ||
| rpc: RPCProtocol, | ||
| container: interfaces.Container | ||
| ) { | ||
| this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.MCP_SERVER_DEFINITION_REGISTRY_EXT); | ||
| try { | ||
| this.mcpServerManager = container.get(MCPServerManager); | ||
| } catch { | ||
| // MCP Server Manager is optional | ||
| this.mcpServerManager = undefined; | ||
| } | ||
| } | ||
|
|
||
| $registerMcpServerDefinitionProvider(handle: number, name: string): void { | ||
| this.providers.set(handle, name); | ||
| // Trigger initial load of server definitions | ||
| this.loadServerDefinitions(handle); | ||
| } | ||
|
|
||
| $unregisterMcpServerDefinitionProvider(handle: number): void { | ||
| if (!this.mcpServerManager) { | ||
| console.warn('MCP Server Manager not available - MCP server definitions will not be loaded'); | ||
| return; | ||
| } | ||
| const provider = this.providers.get(handle); | ||
| if (!provider) { | ||
| console.warn(`No MCP Server provider found for handle '${handle}' - MCP server definitions will not be loaded`); | ||
| return; | ||
| } | ||
| this.mcpServerManager.removeServer(provider); | ||
| this.providers.delete(handle); | ||
| } | ||
|
|
||
| $onDidChangeMcpServerDefinitions(handle: number): void { | ||
| // Reload server definitions when provider reports changes | ||
| this.loadServerDefinitions(handle); | ||
| } | ||
|
|
||
| async $getServerDefinitions(handle: number): Promise<McpServerDefinitionDto[]> { | ||
| try { | ||
| return await this.proxy.$provideServerDefinitions(handle); | ||
| } catch (error) { | ||
| console.error('Error getting MCP server definitions:', error); | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| async $resolveServerDefinition(handle: number, server: McpServerDefinitionDto): Promise<McpServerDefinitionDto | undefined> { | ||
| try { | ||
| return await this.proxy.$resolveServerDefinition(handle, server); | ||
| } catch (error) { | ||
| console.error('Error resolving MCP server definition:', error); | ||
| return server; | ||
| } | ||
| } | ||
|
|
||
| private async loadServerDefinitions(handle: number): Promise<void> { | ||
| if (!this.mcpServerManager) { | ||
| console.warn('MCP Server Manager not available - MCP server definitions will not be loaded'); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const definitions = await this.$getServerDefinitions(handle); | ||
|
|
||
| for (const definition of definitions) { | ||
| const resolved = await this.$resolveServerDefinition(handle, definition); | ||
| if (resolved) { | ||
| const mcpServerDescription = this.convertToMcpServerDescription(resolved); | ||
| this.mcpServerManager.addOrUpdateServer(mcpServerDescription); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error('Error loading MCP server definitions:', error); | ||
| } | ||
| } | ||
|
|
||
| private convertToMcpServerDescription(definition: McpServerDefinitionDto): MCPServerDescription { | ||
| if (isMcpHttpServerDefinitionDto(definition)) { | ||
| // For HTTP servers, we would need to create a bridge or adapter | ||
| // For now, we'll create a placeholder stdio server that could proxy to HTTP | ||
| console.warn(`HTTP transport not yet supported for MCP server '${definition.label}'. Skipping.`); | ||
| throw new Error(`HTTP transport not yet supported for MCP server '${definition.label}'`); | ||
| } | ||
|
|
||
| // Convert env values to strings, filtering out null values | ||
| let convertedEnv: Record<string, string> | undefined; | ||
| if (definition.env) { | ||
| convertedEnv = {}; | ||
| for (const [key, value] of Object.entries(definition.env)) { | ||
| if (value !== null) { | ||
| convertedEnv[key] = String(value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| name: definition.label, | ||
| command: definition.command!, | ||
| args: definition.args, | ||
| env: convertedEnv, | ||
| autostart: false, // Extensions should manage their own server lifecycle | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.