Skip to content

Commit d07a5bc

Browse files
committed
Add initial support for MCP
fixed #14523 Signed-off-by: Jonas Helming <[email protected]>
1 parent 2078cdf commit d07a5bc

20 files changed

+875
-8
lines changed

examples/browser/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@theia/ai-history": "1.56.0",
3030
"@theia/ai-huggingface": "1.56.0",
3131
"@theia/ai-llamafile": "1.56.0",
32+
"@theia/ai-mcp": "1.56.0",
3233
"@theia/ai-ollama": "1.56.0",
3334
"@theia/ai-openai": "1.56.0",
3435
"@theia/ai-terminal": "1.56.0",

examples/browser/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
{
3030
"path": "../../packages/ai-llamafile"
3131
},
32+
{
33+
"path": "../../packages/ai-mcp"
34+
},
3235
{
3336
"path": "../../packages/ai-ollama"
3437
},

packages/ai-core/src/browser/prompttemplate-contribution.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
1515
// *****************************************************************************
1616

17-
import { inject, injectable, named } from '@theia/core/shared/inversify';
17+
import { inject, injectable } from '@theia/core/shared/inversify';
1818
import { GrammarDefinition, GrammarDefinitionProvider, LanguageGrammarDefinitionContribution, TextmateRegistry } from '@theia/monaco/lib/browser/textmate';
1919
import * as monaco from '@theia/monaco-editor-core';
20-
import { Command, CommandContribution, CommandRegistry, ContributionProvider, MessageService } from '@theia/core';
20+
import { Command, CommandContribution, CommandRegistry, MessageService } from '@theia/core';
2121
import { TabBarToolbarContribution, TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
2222

2323
import { codicon, Widget } from '@theia/core/lib/browser';
2424
import { EditorWidget, ReplaceOperation } from '@theia/editor/lib/browser';
25-
import { PromptCustomizationService, PromptService, ToolProvider } from '../common';
25+
import { PromptCustomizationService, PromptService, ToolInvocationRegistry } from '../common';
2626
import { ProviderResult } from '@theia/monaco-editor-core/esm/vs/editor/common/languages';
2727

2828
const PROMPT_TEMPLATE_LANGUAGE_ID = 'theia-ai-prompt-template';
@@ -56,9 +56,8 @@ export class PromptTemplateContribution implements LanguageGrammarDefinitionCont
5656
@inject(PromptCustomizationService)
5757
protected readonly customizationService: PromptCustomizationService;
5858

59-
@inject(ContributionProvider)
60-
@named(ToolProvider)
61-
private toolProviders: ContributionProvider<ToolProvider>;
59+
@inject(ToolInvocationRegistry)
60+
protected readonly toolInvocationRegistry: ToolInvocationRegistry;
6261

6362
readonly config: monaco.languages.LanguageConfiguration =
6463
{
@@ -115,7 +114,7 @@ export class PromptTemplateContribution implements LanguageGrammarDefinitionCont
115114
model,
116115
position,
117116
'~{',
118-
this.toolProviders.getContributions().map(provider => provider.getTool()),
117+
this.toolInvocationRegistry.getAllFunctions(),
119118
monaco.languages.CompletionItemKind.Function,
120119
tool => tool.id,
121120
tool => tool.name,

packages/ai-core/src/common/language-model.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface ToolRequest {
3838
parameters?: { type?: 'object', properties: Record<string, { type: string, [key: string]: unknown }> };
3939
description?: string;
4040
handler: (arg_string: string) => Promise<unknown>;
41+
providerName?: string;
4142
}
4243
export interface LanguageModelRequest {
4344
messages: LanguageModelRequestMessage[],

packages/ai-core/src/common/tool-invocation-registry.ts

+46
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,44 @@ export const ToolInvocationRegistry = Symbol('ToolInvocationRegistry');
2424
* Registry for all the function calls available to Agents.
2525
*/
2626
export interface ToolInvocationRegistry {
27+
/**
28+
* Registers a tool into the registry.
29+
*
30+
* @param tool - The `ToolRequest` object representing the tool to be registered.
31+
*/
2732
registerTool(tool: ToolRequest): void;
2833

34+
/**
35+
* Retrieves a specific `ToolRequest` from the registry.
36+
*
37+
* @param toolId - The unique identifier of the tool to retrieve.
38+
* @returns The `ToolRequest` object corresponding to the provided tool ID,
39+
* or `undefined` if the tool is not found in the registry.
40+
*/
2941
getFunction(toolId: string): ToolRequest | undefined;
3042

43+
/**
44+
* Retrieves multiple `ToolRequest`s configurations from the registry.
45+
*
46+
* @param toolIds - A list of tool IDs to retrieve.
47+
* @returns An array of `ToolRequest` objects for the specified tool IDs.
48+
* If a tool ID is not found, it is skipped in the returned array.
49+
*/
3150
getFunctions(...toolIds: string[]): ToolRequest[];
51+
52+
/**
53+
* Retrieves all `ToolRequest`s currently registered in the registry.
54+
*
55+
* @returns An array of all `ToolRequest` objects in the registry.
56+
*/
57+
getAllFunctions(): ToolRequest[];
58+
59+
/**
60+
* Unregisters all tools provided by a specific tool provider.
61+
*
62+
* @param providerName - The name of the tool provider whose tools should be removed (as specificed in the `ToolRequest`).
63+
*/
64+
unregisterAllTools(providerName: string): void;
3265
}
3366

3467
export const ToolProvider = Symbol('ToolProvider');
@@ -52,6 +85,19 @@ export class ToolInvocationRegistryImpl implements ToolInvocationRegistry {
5285
});
5386
}
5487

88+
unregisterAllTools(providerName: string): void {
89+
const toolsToRemove: string[] = [];
90+
for (const [id, tool] of this.tools.entries()) {
91+
if (tool.providerName === providerName) {
92+
toolsToRemove.push(id);
93+
}
94+
}
95+
toolsToRemove.forEach(id => this.tools.delete(id));
96+
}
97+
getAllFunctions(): ToolRequest[] {
98+
return Array.from(this.tools.values());
99+
}
100+
55101
registerTool(tool: ToolRequest): void {
56102
if (this.tools.has(tool.id)) {
57103
console.warn(`Function with id ${tool.id} is already registered.`);

packages/ai-mcp/README.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Model Context Server (MCP) Integration
2+
3+
The AI MCP package provides an integration that allows users to start and use MCP Servers to provide additional tool functions to LLMs, e.g. search or file access (outside of the workspace).
4+
5+
## Features
6+
- Add MCP Servers via settings.json
7+
- Start and stop MCP servers.
8+
- Use tool functions provided by MCP servers in prompt templates
9+
10+
## Commands
11+
12+
### Start MCP Server
13+
14+
- **Command ID:** `mcp.startserver`
15+
- **Label:** `MCP: Start MCP Server`
16+
- **Functionality:** Allows you to start a MCP server by selecting from a list of configured servers.
17+
18+
### Stop MCP Server
19+
20+
- **Command ID:** `mcp.stopserver`
21+
- **Label:** `MCP: Stop MCP Server`
22+
- **Functionality:** Allows you to stop a running MCP server by selecting from a list of currently running servers.
23+
24+
## Usage
25+
26+
1. **Starting a Llamafile Language Server:**
27+
28+
- Use the command palette to invoke `MCP: Start MCP Server`.
29+
- A quick pick menu will appear with a list of configured MCP Servers.
30+
- Select a server to start.
31+
32+
2. **Stopping a Llamafile Language Server:**
33+
- Use the command palette to invoke `MCP: Stop MCP Server`.
34+
- A quick pick menu will display a list of currently running MCP Servers.
35+
- Select a server to stop.
36+
37+
3. **Using provided tool functions**
38+
- Only functions of started MCP servers can be used
39+
- Open a prompt template and add the added tool functions
40+
- Type '~{' to open the auto completion
41+
42+
## Configuration
43+
44+
Make sure to configure your MCP Servers properly within the preference settings.
45+
46+
Example Configuration:
47+
48+
```json
49+
{
50+
"ai-features.mcp.mcpServers": {
51+
"memory": {
52+
"command": "npx",
53+
"args": [
54+
"-y",
55+
"@modelcontextprotocol/server-memory"
56+
]
57+
},
58+
"brave-search": {
59+
"command": "npx",
60+
"args": [
61+
"-y",
62+
"@modelcontextprotocol/server-brave-search"
63+
],
64+
"env": {
65+
"BRAVE_API_KEY": "YOUR_API_KEY"
66+
}
67+
},
68+
"filesystem": {
69+
"command": "npx",
70+
"args": [
71+
"-y",
72+
"@modelcontextprotocol/server-filesystem",
73+
"ABSOLUTE_PATH_TO_ALLOWED_DIRECTORY",
74+
]
75+
},
76+
}
77+
}
78+
```
79+
80+
Example prompt (for search)
81+
```md
82+
~{mcp_brave-search_brave_web_search}
83+
```
84+
85+
Example User query
86+
```md
87+
Search the internet for XYZ
88+
```
89+
90+
## More Information
91+
[List of available MCP servers](https://github.com/modelcontextprotocol/servers)

packages/ai-mcp/package.json

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "@theia/ai-mcp",
3+
"version": "1.56.0",
4+
"description": "Theia - MCP Integration",
5+
"dependencies": {
6+
"@theia/core": "1.56.0",
7+
"@theia/ai-core": "1.56.0",
8+
"@modelcontextprotocol/sdk": "1.0.1"
9+
},
10+
"publishConfig": {
11+
"access": "public"
12+
},
13+
"theiaExtensions": [
14+
{
15+
"frontend": "lib/browser/ai-mcp-frontend-module",
16+
"backend": "lib/node/ai-mcp-backend-module"
17+
}
18+
],
19+
"keywords": [
20+
"theia-extension"
21+
],
22+
"license": "EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0",
23+
"repository": {
24+
"type": "git",
25+
"url": "https://github.com/eclipse-theia/theia.git"
26+
},
27+
"bugs": {
28+
"url": "https://github.com/eclipse-theia/theia/issues"
29+
},
30+
"homepage": "https://github.com/eclipse-theia/theia",
31+
"files": [
32+
"lib",
33+
"src"
34+
],
35+
"scripts": {
36+
"build": "theiaext build",
37+
"clean": "theiaext clean",
38+
"compile": "theiaext compile",
39+
"lint": "theiaext lint",
40+
"test": "theiaext test",
41+
"watch": "theiaext watch"
42+
},
43+
"devDependencies": {
44+
"@theia/ext-scripts": "1.56.0"
45+
},
46+
"nyc": {
47+
"extends": "../../configs/nyc.json"
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// *****************************************************************************
2+
// Copyright (C) 2024 EclipseSource GmbH.
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License v. 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0.
7+
//
8+
// This Source Code may also be made available under the following Secondary
9+
// Licenses when the conditions for such availability set forth in the Eclipse
10+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11+
// with the GNU Classpath Exception which is available at
12+
// https://www.gnu.org/software/classpath/license.html.
13+
//
14+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15+
// *****************************************************************************
16+
import { AICommandHandlerFactory } from '@theia/ai-core/lib/browser/ai-command-handler-factory';
17+
import { CommandContribution, CommandRegistry, MessageService } from '@theia/core';
18+
import { QuickInputService } from '@theia/core/lib/browser';
19+
import { inject, injectable } from '@theia/core/shared/inversify';
20+
import { MCPServerManager } from '../common/mcp-server-manager';
21+
import { ToolInvocationRegistry, ToolRequest } from '@theia/ai-core';
22+
23+
export const StartMCPServer = {
24+
id: 'mcp.startserver',
25+
label: 'MCP: Start MCP Server',
26+
};
27+
export const StopMCPServer = {
28+
id: 'mcp.stopserver',
29+
label: 'MCP: Stop MCP Server',
30+
};
31+
32+
@injectable()
33+
export class MCPCommandContribution implements CommandContribution {
34+
@inject(AICommandHandlerFactory)
35+
protected readonly commandHandlerFactory: AICommandHandlerFactory;
36+
37+
@inject(QuickInputService)
38+
protected readonly quickInputService: QuickInputService;
39+
40+
@inject(MessageService)
41+
protected messageService: MessageService;
42+
43+
@inject(MCPServerManager)
44+
protected readonly mcpServerManager: MCPServerManager;
45+
46+
@inject(ToolInvocationRegistry)
47+
protected readonly toolInvocationRegistry: ToolInvocationRegistry;
48+
49+
50+
private async getMCPServerSelection(serverNames: String[]): Promise<string | undefined> {
51+
if (!serverNames || serverNames.length === 0) {
52+
this.messageService.error('No MCP Servers configured.');
53+
return undefined;
54+
}
55+
const options = serverNames.map(mcpServerName => ({ label: mcpServerName as string }));
56+
const result = await this.quickInputService.showQuickPick(options);
57+
if (!result) {
58+
return undefined;
59+
}
60+
return result.label;
61+
}
62+
63+
registerCommands(commandRegistry: CommandRegistry): void {
64+
commandRegistry.registerCommand(StopMCPServer, this.commandHandlerFactory({
65+
execute: async () => {
66+
try {
67+
const selection = await this.getMCPServerSelection(await this.mcpServerManager.getStartedServers());
68+
if (!selection) {
69+
return;
70+
}
71+
this.toolInvocationRegistry.unregisterAllTools(`mcp_${selection}`);
72+
this.mcpServerManager.stopServer(selection);
73+
} catch (error) {
74+
console.error('Error while stopping MCP server:', error);
75+
}
76+
}
77+
}));
78+
79+
commandRegistry.registerCommand(StartMCPServer, this.commandHandlerFactory({
80+
execute: async () => {
81+
try {
82+
const selection = await this.getMCPServerSelection(await this.mcpServerManager.getServerNames());
83+
if (!selection) {
84+
return;
85+
}
86+
this.mcpServerManager.startServer(selection);
87+
const { tools } = await this.mcpServerManager.getTools(selection);
88+
const toolRequests: ToolRequest[] = tools.map((tool: any) => this.convertToToolRequest(tool, selection));
89+
90+
for (const toolRequest of toolRequests) {
91+
this.toolInvocationRegistry.registerTool(toolRequest);
92+
}
93+
} catch (error) {
94+
console.error('Error while starting MCP server:', error);
95+
}
96+
}
97+
}));
98+
}
99+
100+
convertToToolRequest(tool: any, serverName: string): ToolRequest {
101+
const id = `mcp_${serverName}_${tool.name}`;
102+
return {
103+
id: id,
104+
name: id,
105+
providerName: `mcp_${serverName}`,
106+
parameters: tool.inputSchema ? {
107+
type: tool.inputSchema.type,
108+
properties: tool.inputSchema.properties,
109+
} : undefined,
110+
description: tool.description,
111+
handler: async (arg_string: string) => {
112+
try {
113+
return await this.mcpServerManager.callTool(serverName, tool.name, arg_string);
114+
} catch (error) {
115+
console.error(`Error in tool handler for ${tool.name} on server ${serverName}:`, error);
116+
throw error;
117+
}
118+
},
119+
};
120+
}
121+
122+
}

0 commit comments

Comments
 (0)