-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed #14523 Signed-off-by: Jonas Helming <jhelming@eclipsesource.com>
- Loading branch information
1 parent
2078cdf
commit 251519c
Showing
19 changed files
with
847 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,91 @@ | ||
# Model Context Server (MCP) Integration | ||
|
||
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). | ||
|
||
## Features | ||
- Add MCP Servers via settings.json | ||
- Start and stop MCP servers. | ||
- Use tool functions provided by MCP servers in prompt templates | ||
|
||
## Commands | ||
|
||
### Start MCP Server | ||
|
||
- **Command ID:** `mcp.startserver` | ||
- **Label:** `MCP: Start MCP Server` | ||
- **Functionality:** Allows you to start a MCP server by selecting from a list of configured servers. | ||
|
||
### Stop MCP Server | ||
|
||
- **Command ID:** `mcp.stopserver` | ||
- **Label:** `MCP: Stop MCP Server` | ||
- **Functionality:** Allows you to stop a running MCP server by selecting from a list of currently running servers. | ||
|
||
## Usage | ||
|
||
1. **Starting a Llamafile Language Server:** | ||
|
||
- Use the command palette to invoke `MCP: Start MCP Server`. | ||
- A quick pick menu will appear with a list of configured MCP Servers. | ||
- Select a server to start. | ||
|
||
2. **Stopping a Llamafile Language Server:** | ||
- Use the command palette to invoke `MCP: Stop MCP Server`. | ||
- A quick pick menu will display a list of currently running MCP Servers. | ||
- Select a server to stop. | ||
|
||
3. **Using provided tool functions** | ||
- Only functions of started MCP servers can be used | ||
- Open a prompt template and add the added tool functions | ||
- Type '~{' to open the auto completion | ||
|
||
## Configuration | ||
|
||
Make sure to configure your MCP Servers properly within the preference settings. | ||
|
||
Example Configuration: | ||
|
||
```json | ||
{ | ||
"ai-features.mcp.mcpServers": { | ||
"memory": { | ||
"command": "npx", | ||
"args": [ | ||
"-y", | ||
"@modelcontextprotocol/server-memory" | ||
] | ||
}, | ||
"brave-search": { | ||
"command": "npx", | ||
"args": [ | ||
"-y", | ||
"@modelcontextprotocol/server-brave-search" | ||
], | ||
"env": { | ||
"BRAVE_API_KEY": "YOUR_API_KEY" | ||
} | ||
}, | ||
"filesystem": { | ||
"command": "npx", | ||
"args": [ | ||
"-y", | ||
"@modelcontextprotocol/server-filesystem", | ||
"ABSOLUTE_PATH_TO_ALLOWED_DIRECTORY", | ||
] | ||
}, | ||
} | ||
} | ||
``` | ||
|
||
Example prompt (for search) | ||
```md | ||
~{mcp_brave-search_brave_web_search} | ||
``` | ||
|
||
Example User query | ||
```md | ||
Search the internet for XYZ | ||
``` | ||
|
||
## More Information | ||
[List of available MCP servers](https://github.com/modelcontextprotocol/servers) |
This file contains 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,49 @@ | ||
{ | ||
"name": "@theia/ai-mcp", | ||
"version": "1.56.0", | ||
"description": "Theia - MCP Integration", | ||
"dependencies": { | ||
"@theia/core": "1.56.0", | ||
"@theia/ai-core": "1.56.0", | ||
"@modelcontextprotocol/sdk": "1.0.1" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"theiaExtensions": [ | ||
{ | ||
"frontend": "lib/browser/ai-mcp-frontend-module", | ||
"backend": "lib/node/ai-mcp-backend-module" | ||
} | ||
], | ||
"keywords": [ | ||
"theia-extension" | ||
], | ||
"license": "EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/eclipse-theia/theia.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/eclipse-theia/theia/issues" | ||
}, | ||
"homepage": "https://github.com/eclipse-theia/theia", | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"scripts": { | ||
"build": "theiaext build", | ||
"clean": "theiaext clean", | ||
"compile": "theiaext compile", | ||
"lint": "theiaext lint", | ||
"test": "theiaext test", | ||
"watch": "theiaext watch" | ||
}, | ||
"devDependencies": { | ||
"@theia/ext-scripts": "1.56.0" | ||
}, | ||
"nyc": { | ||
"extends": "../../configs/nyc.json" | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
packages/ai-mcp/src/browser/ai-mcp-command-contribution.ts
This file contains 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,122 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 EclipseSource GmbH. | ||
// | ||
// 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 { AICommandHandlerFactory } from '@theia/ai-core/lib/browser/ai-command-handler-factory'; | ||
import { CommandContribution, CommandRegistry, MessageService } from '@theia/core'; | ||
import { QuickInputService } from '@theia/core/lib/browser'; | ||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { MCPServerManager } from '../common/mcp-server-manager'; | ||
import { ToolInvocationRegistry, ToolRequest } from '@theia/ai-core'; | ||
|
||
export const StartMCPServer = { | ||
id: 'mcp.startserver', | ||
label: 'MCP: Start MCP Server', | ||
}; | ||
export const StopMCPServer = { | ||
id: 'mcp.stopserver', | ||
label: 'MCP: Stop MCP Server', | ||
}; | ||
|
||
@injectable() | ||
export class MCPCommandContribution implements CommandContribution { | ||
@inject(AICommandHandlerFactory) | ||
protected readonly commandHandlerFactory: AICommandHandlerFactory; | ||
|
||
@inject(QuickInputService) | ||
protected readonly quickInputService: QuickInputService; | ||
|
||
@inject(MessageService) | ||
protected messageService: MessageService; | ||
|
||
@inject(MCPServerManager) | ||
protected readonly mcpServerManager: MCPServerManager; | ||
|
||
@inject(ToolInvocationRegistry) | ||
protected readonly toolInvocationRegistry: ToolInvocationRegistry; | ||
|
||
|
||
private async getMCPServerSelection(serverNames: String[]): Promise<string | undefined> { | ||
if (!serverNames || serverNames.length === 0) { | ||
this.messageService.error('No MCP Servers configured.'); | ||
return undefined; | ||
} | ||
const options = serverNames.map(mcpServerName => ({ label: mcpServerName as string })); | ||
const result = await this.quickInputService.showQuickPick(options); | ||
if (!result) { | ||
return undefined; | ||
} | ||
return result.label; | ||
} | ||
|
||
registerCommands(commandRegistry: CommandRegistry): void { | ||
commandRegistry.registerCommand(StopMCPServer, this.commandHandlerFactory({ | ||
execute: async () => { | ||
try { | ||
const selection = await this.getMCPServerSelection(await this.mcpServerManager.getStartedServers()); | ||
if (!selection) { | ||
return; | ||
} | ||
this.toolInvocationRegistry.unregisterAllTools(`mcp_${selection}`); | ||
this.mcpServerManager.stopServer(selection); | ||
} catch (error) { | ||
console.error('Error while stopping MCP server:', error); | ||
} | ||
} | ||
})); | ||
|
||
commandRegistry.registerCommand(StartMCPServer, this.commandHandlerFactory({ | ||
execute: async () => { | ||
try { | ||
const selection = await this.getMCPServerSelection(await this.mcpServerManager.getServerNames()); | ||
if (!selection) { | ||
return; | ||
} | ||
this.mcpServerManager.startServer(selection); | ||
const { tools } = await this.mcpServerManager.getTools(selection); | ||
const toolRequests: ToolRequest[] = tools.map((tool: any) => this.convertToToolRequest(tool, selection)); | ||
|
||
for (const toolRequest of toolRequests) { | ||
this.toolInvocationRegistry.registerTool(toolRequest); | ||
} | ||
} catch (error) { | ||
console.error('Error while starting MCP server:', error); | ||
} | ||
} | ||
})); | ||
} | ||
|
||
convertToToolRequest(tool: any, serverName: string): ToolRequest { | ||
const id = `mcp_${serverName}_${tool.name}`; | ||
return { | ||
id: id, | ||
name: id, | ||
providerName: `mcp_${serverName}`, | ||
parameters: tool.inputSchema ? { | ||
type: tool.inputSchema.type, | ||
properties: tool.inputSchema.properties, | ||
} : undefined, | ||
description: tool.description, | ||
handler: async (arg_string: string) => { | ||
try { | ||
return await this.mcpServerManager.callTool(serverName, tool.name, arg_string); | ||
} catch (error) { | ||
console.error(`Error in tool handler for ${tool.name} on server ${serverName}:`, error); | ||
throw error; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
} |
Oops, something went wrong.