Skip to content

Commit

Permalink
Add initial support for MCP
Browse files Browse the repository at this point in the history
fixed #14523

Signed-off-by: Jonas Helming <jhelming@eclipsesource.com>
  • Loading branch information
JonasHelming committed Dec 8, 2024
1 parent 2078cdf commit 251519c
Show file tree
Hide file tree
Showing 19 changed files with 847 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@theia/ai-history": "1.56.0",
"@theia/ai-huggingface": "1.56.0",
"@theia/ai-llamafile": "1.56.0",
"@theia/ai-mcp": "1.56.0",
"@theia/ai-ollama": "1.56.0",
"@theia/ai-openai": "1.56.0",
"@theia/ai-terminal": "1.56.0",
Expand Down
3 changes: 3 additions & 0 deletions examples/browser/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
{
"path": "../../packages/ai-llamafile"
},
{
"path": "../../packages/ai-mcp"
},
{
"path": "../../packages/ai-ollama"
},
Expand Down
13 changes: 6 additions & 7 deletions packages/ai-core/src/browser/prompttemplate-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

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

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

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

@inject(ContributionProvider)
@named(ToolProvider)
private toolProviders: ContributionProvider<ToolProvider>;
@inject(ToolInvocationRegistry)
protected readonly toolInvocationRegistry: ToolInvocationRegistry;

readonly config: monaco.languages.LanguageConfiguration =
{
Expand Down Expand Up @@ -115,7 +114,7 @@ export class PromptTemplateContribution implements LanguageGrammarDefinitionCont
model,
position,
'~{',
this.toolProviders.getContributions().map(provider => provider.getTool()),
this.toolInvocationRegistry.getAllFunctions(),
monaco.languages.CompletionItemKind.Function,
tool => tool.id,
tool => tool.name,
Expand Down
1 change: 1 addition & 0 deletions packages/ai-core/src/common/language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface ToolRequest {
parameters?: { type?: 'object', properties: Record<string, { type: string, [key: string]: unknown }> };
description?: string;
handler: (arg_string: string) => Promise<unknown>;
providerName?: string;
}
export interface LanguageModelRequest {
messages: LanguageModelRequestMessage[],
Expand Down
46 changes: 46 additions & 0 deletions packages/ai-core/src/common/tool-invocation-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,44 @@ export const ToolInvocationRegistry = Symbol('ToolInvocationRegistry');
* Registry for all the function calls available to Agents.
*/
export interface ToolInvocationRegistry {
/**
* Registers a tool into the registry.
*
* @param tool - The `ToolRequest` object representing the tool to be registered.
*/
registerTool(tool: ToolRequest): void;

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

/**
* Retrieves multiple `ToolRequest`s configurations from the registry.
*
* @param toolIds - A list of tool IDs to retrieve.
* @returns An array of `ToolRequest` objects for the specified tool IDs.
* If a tool ID is not found, it is skipped in the returned array.
*/
getFunctions(...toolIds: string[]): ToolRequest[];

/**
* Retrieves all `ToolRequest`s currently registered in the registry.
*
* @returns An array of all `ToolRequest` objects in the registry.
*/
getAllFunctions(): ToolRequest[];

/**
* Unregisters all tools provided by a specific tool provider.
*
* @param providerName - The name of the tool provider whose tools should be removed (as specificed in the `ToolRequest`).
*/
unregisterAllTools(providerName: string): void;
}

export const ToolProvider = Symbol('ToolProvider');
Expand All @@ -52,6 +85,19 @@ export class ToolInvocationRegistryImpl implements ToolInvocationRegistry {
});
}

unregisterAllTools(providerName: string): void {
const toolsToRemove: string[] = [];
for (const [id, tool] of this.tools.entries()) {
if (tool.providerName === providerName) {
toolsToRemove.push(id);
}
}
toolsToRemove.forEach(id => this.tools.delete(id));
}
getAllFunctions(): ToolRequest[] {
return Array.from(this.tools.values());
}

registerTool(tool: ToolRequest): void {
if (this.tools.has(tool.id)) {
console.warn(`Function with id ${tool.id} is already registered.`);
Expand Down
91 changes: 91 additions & 0 deletions packages/ai-mcp/README.md
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)
49 changes: 49 additions & 0 deletions packages/ai-mcp/package.json
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 packages/ai-mcp/src/browser/ai-mcp-command-contribution.ts
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;
}
},
};
}

}
Loading

0 comments on commit 251519c

Please sign in to comment.