-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support MCP server and client (#4335)
* feat: mcp server client poc * feat: introduce MCP tools contribution * fix: 修复 mcp sdk 引入类型问题 * feat: add builtin MCP server * fix: mcp types fix * fix: mcp types fix2 * feat: sumi mcp builtin sever * feat: code optimization * feat: support llm tool call streaming and ui, more mcp tools * feat: enhance language model error handling and streaming * feat: mcp tools grouped by clientId, add mcp tools panel * feat: add openai compatible api preferences * feat: support chat history in language model request * feat: add MCP server configuration support via preferences * feat: implement readfile & readdir tools * fix: tool impl bugs * refactor: use design system variables in ChatToolRender styles * refactor: improve logging and revert some unnecessary optimization * fix: logger not work in node.js * fix: mcp tool render fix * feat: add MCP and custom LLM config * fix: build error fix * fix: lint fix * fix: lint fix * fix: lint error fix * feat: format the tool call error message * feat: add doc, lint fix, create file tool fix * fix: lint fix * feat: add llmcontext service (#4374) * feat: add llmcontext service * feat: integrate rc-collapse for improved chat context UI * feat: implement ChatAgentPromptProvider for enhanced context handling * feat: update chat agent prompt provider to use serialized context * fix: build error * feat: close selector when click outside * chore: import order * feat: run terminal cmd tool (#4383) * chore: update terminal * chore: update exit code * feat: enhance terminal command execution with success message and auto-close feature * feat: add some unit tests --------- Co-authored-by: ensorrow <[email protected]> Co-authored-by: 大表哥 <[email protected]> Co-authored-by: liuqian <[email protected]>
- Loading branch information
1 parent
dacfb46
commit ff995f5
Showing
72 changed files
with
5,679 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,4 +98,5 @@ tools/workspace | |
# jupyter | ||
.ipynb_checkpoints | ||
|
||
*.tsbuildinfo | ||
*.tsbuildinfo | ||
.env |
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,225 @@ | ||
# Model Control Protocol (MCP) Documentation | ||
|
||
## Overview | ||
|
||
The Model Control Protocol (MCP) is an integration layer that enables IDE capabilities to be exposed to AI models through a standardized interface. It provides a set of tools that allow AI models to interact with the IDE environment, manipulate files, and perform various operations. | ||
|
||
## Architecture | ||
|
||
Component Relationships: | ||
|
||
``` | ||
┌─────────────────────┐ | ||
│ MCPServerManager │ | ||
│ (Per Browser Tab) │ | ||
└─────────┬───────────┘ | ||
│ | ||
│ manages | ||
▼ | ||
┌─────────────────────┐ ┌───────────────────┐ | ||
│ MCPServerRegistry │◄──────────┤ Builtin/External │ | ||
│ (Frontend Proxy) │ register │ MCP Servers │ | ||
└─────────┬───────────┘ tools └───────────────────┘ | ||
│ | ||
│ forwards | ||
▼ | ||
┌─────────────────────┐ ┌─────────────────────────────┐ | ||
│ SumiMCPServerBackend│◄──────────┤ ToolInvocationRegistryManager│ | ||
│ (Browser<->Node.js)│ uses │ (Registry per Client) │ | ||
└─────────┬───────────┘ └─────────────┬───────────────┘ | ||
│ │ | ||
│ executes │ manages | ||
▼ ▼ | ||
┌─────────────────────┐ ┌─────────────────────────┐ | ||
│ Tool Handlers │ │ ToolInvocationRegistry │ | ||
│ (Implementation) │ │ (Available Tools) │ | ||
└─────────────────────┘ └─────────────────────────┘ | ||
``` | ||
|
||
### Core Components | ||
|
||
1. **MCPServerManager** | ||
|
||
- Manages multiple MCP servers | ||
- Handles tool registration and invocation | ||
- Maintains server lifecycle (start/stop) | ||
- Each browser tab has its own MCPServerManager instance | ||
|
||
2. **MCPServerRegistry** | ||
|
||
- Frontend proxy service for MCP | ||
- Registers and manages MCP tools | ||
- Handles tool invocations | ||
|
||
3. **SumiMCPServerBackend** | ||
|
||
- Backend service that bridges browser and Node.js layers | ||
- Manages tool registration and invocation | ||
- Handles communication between frontend and backend | ||
|
||
4. **ToolInvocationRegistry** | ||
|
||
- Registry for all available function calls for agents | ||
- Manages tool registration and lookup | ||
- Maintains a map of tool IDs to their implementations | ||
- Supports tool registration, retrieval, and unregistration | ||
|
||
5. **ToolInvocationRegistryManager** | ||
- Manages multiple ToolInvocationRegistry instances | ||
- Each instance is associated with a specific clientId | ||
- Provides registry creation, retrieval, and removal | ||
- Ensures isolation between different client contexts | ||
|
||
### Server Types | ||
|
||
1. **Builtin MCP Server** | ||
|
||
- Provides core IDE capabilities | ||
- Integrated directly into the IDE | ||
|
||
2. **External MCP Servers** | ||
- Can be added dynamically | ||
- Configured with name, command, args, and environment variables | ||
|
||
## Available Tools | ||
|
||
The MCP system provides several built-in tools for file and IDE operations: | ||
|
||
### File Operations | ||
|
||
- `readFile`: Read contents of a file with line range support | ||
- `listDir`: List contents of a directory | ||
- `createNewFileWithText`: Create a new file with specified content | ||
- `findFilesByNameSubstring`: Search for files by name | ||
- `getFileTextByPath`: Get the content of a file by path | ||
- `replaceOpenEditorFile`: Replace content in the current editor | ||
- `replaceOpenEditorFileByDiffPreviewer`: Replace content with diff preview | ||
|
||
### Editor Operations | ||
|
||
- `getCurrentFilePath`: Get path of current open file | ||
- `getSelectedText`: Get currently selected text | ||
- `getOpenEditorFileText`: Get text from open editor | ||
|
||
### Diagnostics | ||
|
||
- `getDiagnosticsByPath`: Get diagnostics for a specific file | ||
- `getOpenEditorFileDiagnostics`: Get diagnostics for open editor | ||
|
||
## Tool Structure | ||
|
||
Each MCP tool follows a standard structure: | ||
|
||
```typescript | ||
interface MCPTool { | ||
name: string; | ||
description: string; | ||
inputSchema: any; | ||
providerName: string; | ||
} | ||
|
||
interface MCPToolDefinition { | ||
name: string; | ||
description: string; | ||
inputSchema: any; | ||
handler: ( | ||
args: any, | ||
logger: MCPLogger, | ||
) => Promise<{ | ||
content: { type: string; text: string }[]; | ||
isError?: boolean; | ||
}>; | ||
} | ||
``` | ||
|
||
## Usage Examples | ||
|
||
### Registering a New Tool | ||
|
||
```typescript | ||
@Domain(MCPServerContribution) | ||
export class MyCustomTool implements MCPServerContribution { | ||
registerMCPServer(registry: IMCPServerRegistry): void { | ||
registry.registerMCPTool({ | ||
name: 'my_custom_tool', | ||
description: 'Description of what the tool does', | ||
inputSchema: zodToJsonSchema(myInputSchema), | ||
handler: async (args, logger) => { | ||
// Tool implementation | ||
return { | ||
content: [{ type: 'text', text: 'Result' }], | ||
}; | ||
}, | ||
}); | ||
} | ||
} | ||
``` | ||
|
||
### Adding External MCP Server - Configuration | ||
|
||
You can add external MCP servers through the `ai.native.mcp.servers` configuration in IDE settings. The configuration format is as follows: | ||
|
||
```json | ||
{ | ||
"ai.native.mcp.servers": [ | ||
{ | ||
"name": "server-name", | ||
"command": "command-to-execute", | ||
"args": ["command-arguments"], | ||
"env": { | ||
"ENV_VAR_NAME": "env-var-value" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Example configuration: | ||
|
||
```json | ||
{ | ||
"ai.native.mcp.servers": [ | ||
{ | ||
"name": "filesystem", | ||
"command": "npx", | ||
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/workspace"], | ||
"env": {} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Best Practices | ||
|
||
1. **Tool Implementation** | ||
|
||
- Always validate input using schemas (e.g., Zod) | ||
- Provide clear error messages | ||
- Use the logger for debugging and tracking | ||
- Handle errors gracefully | ||
|
||
2. **Server Management** | ||
|
||
- Initialize servers only when needed | ||
- Clean up resources when servers are stopped | ||
- Handle server lifecycle events properly | ||
|
||
3. **Tool Usage** | ||
- Check tool availability before use | ||
- Handle tool invocation errors | ||
- Use appropriate tools for specific tasks | ||
- Consider performance implications (e.g., reading entire files vs. line ranges) | ||
|
||
## Error Handling | ||
|
||
Tools should return errors in a standardized format: | ||
|
||
```typescript | ||
{ | ||
content: [{ | ||
type: 'text', | ||
text: 'Error: <error message>' | ||
}], | ||
isError: true | ||
} | ||
``` |
Oops, something went wrong.