Skip to content
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

Add initial support for MCP #14598

Merged
merged 15 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
sdirix marked this conversation as resolved.
Show resolved Hide resolved
*
* @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
10 changes: 10 additions & 0 deletions packages/ai-mcp/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('eslint').Linter.Config} */
module.exports = {
extends: [
'../../configs/build.eslintrc.json'
],
parserOptions: {
tsconfigRootDir: __dirname,
project: 'tsconfig.json'
}
};
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).
sdirix marked this conversation as resolved.
Show resolved Hide resolved

## Features
- Add MCP Servers via settings.json
sdirix marked this conversation as resolved.
Show resolved Hide resolved
- 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:**
sdirix marked this conversation as resolved.
Show resolved Hide resolved

- Use the command palette to invoke `MCP: Start MCP Server`.
- A quick pick menu will appear with a list of configured MCP Servers.
sdirix marked this conversation as resolved.
Show resolved Hide resolved
- Select a server to start.

2. **Stopping a Llamafile Language Server:**
sdirix marked this conversation as resolved.
Show resolved Hide resolved
- Use the command palette to invoke `MCP: Stop MCP Server`.
- A quick pick menu will display a list of currently running MCP Servers.
sdirix marked this conversation as resolved.
Show resolved Hide resolved
- 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.
sdirix marked this conversation as resolved.
Show resolved Hide resolved

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"
JonasHelming marked this conversation as resolved.
Show resolved Hide resolved
}
],
"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"
}
}
Loading
Loading