-
Notifications
You must be signed in to change notification settings - Fork 0
9 feature mcp model context protocol #10
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
863d309
feat: implement Model Context Protocol (MCP) for tool integration
maximedogawa 4960152
feat: enhance MCP integration with filesystem support and dialog plugin
maximedogawa 9b91a3f
feat: enhance agent and MCP client for improved tool handling
maximedogawa 178d721
feat: implement server management in MCP with CRUD operations
maximedogawa 477e8c4
feat: enhance Ollama module with new API functions and types
maximedogawa 8385793
feat: improve tool handling in agent and Ollama service
maximedogawa 803b0b3
feat: enhance MCP and Ollama integration with improved error handling…
maximedogawa 5d68db6
feat: improve MCP server card and error handling in Ollama service
maximedogawa eaa1be5
feat: enhance error handling and user feedback in MCP components
maximedogawa f7d6e0f
feat: enhance MCP server management and error handling
maximedogawa c9066bc
feat: improve error handling and loading states in MCP tools management
maximedogawa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,112 @@ | ||
| # MCP — Model Context Protocol Module (POC) | ||
|
|
||
| > Status: **proof of concept**. One server, one transport, one happy path. | ||
|
|
||
| ## What & Why | ||
|
|
||
| [MCP](https://modelcontextprotocol.org/) is an open JSON-RPC 2.0 protocol that lets an LLM "host" discover and call tools exposed by external "servers". Pengine adopts MCP so we can grow the agent's capabilities by dropping in new servers instead of writing bespoke Rust glue for each tool. Every tool call flows through one well-defined choke point, which is what makes it auditable. | ||
|
|
||
| ## Roles in Pengine | ||
|
|
||
| | Role | Where | Responsibility | | ||
| |---|---|---| | ||
| | **Host** | Pengine (Tauri binary) | Owns the LLM (Ollama) connection, the Telegram bot, and the agent loop. | | ||
| | **Client** | `src-tauri/src/modules/mcp/` | One `McpClient` per connected server. Speaks JSON-RPC over stdio. | | ||
| | **Server** | External child process | Anything that speaks MCP — `npx @modelcontextprotocol/server-filesystem`, a Docker container, a custom binary. | | ||
|
|
||
| ```text | ||
| Telegram message | ||
| │ | ||
| ▼ | ||
| bot::service::text_handler | ||
| │ | ||
| ▼ | ||
| bot::agent::run_turn ────► ollama::chat_with_tools (Ollama /api/chat) | ||
| ▲ │ | ||
| │ │ tool_calls? | ||
| │ ▼ | ||
| └─────────── mcp::registry::call_tool ──► McpClient ──► child process (stdio) | ||
| ``` | ||
|
|
||
| ## Module Layout | ||
|
|
||
| ```text | ||
| src-tauri/src/modules/mcp/ | ||
| ├── mod.rs | ||
| ├── protocol.rs JSON-RPC 2.0 request/response types | ||
| ├── types.rs McpConfig, ServerConfig, Tool | ||
| ├── transport.rs StdioTransport — child process + line-delimited JSON | ||
| ├── client.rs McpClient — initialize / tools/list / tools/call | ||
| ├── registry.rs McpRegistry — fan-out across all connected servers | ||
| └── service.rs load_or_init_config(), connect_all() | ||
| ``` | ||
|
|
||
| The registry lives on `AppState.mcp` (`Arc<RwLock<McpRegistry>>`) so the bot agent and any future HTTP route can reach it. | ||
|
|
||
| ## Config | ||
|
|
||
| File: `$APP_DATA/mcp.json` (next to `connection.json`). Created on first launch with a sane default if missing. | ||
|
|
||
| ```json | ||
| { | ||
| "servers": { | ||
| "filesystem": { | ||
| "command": "npx", | ||
| "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"], | ||
| "env": {} | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| To add a server: add another entry under `servers`. Restart Pengine. | ||
|
|
||
| ## Protocol Subset Implemented | ||
|
|
||
| Four messages, that's it: | ||
|
|
||
| 1. `initialize` — handshake. We send `{protocolVersion, capabilities, clientInfo}` and ignore most of the response. | ||
| 2. `notifications/initialized` — required notification after init. | ||
| 3. `tools/list` — discovery, cached on the client. | ||
| 4. `tools/call` — `{name, arguments}` → `{content: [{type: "text", text}]}`. | ||
|
|
||
| Out of scope for the POC: resources, prompts, sampling, server-initiated requests, batch JSON-RPC, HTTP transport. | ||
|
|
||
| ## Ollama Bridge | ||
|
|
||
| MCP `inputSchema` is JSON Schema, and so are Ollama's tool `parameters` — translation is just a rename. See `to_ollama_tools` in `bot/agent.rs`. Tool names are emitted as `server.tool` so the registry can route a call back to the right client. | ||
|
|
||
| The agent loop in `bot::agent::run_turn`: | ||
|
|
||
| 1. Snapshot the available tools from the registry. | ||
| 2. Send `system + user` plus the tool list to Ollama. | ||
| 3. If the response carries `tool_calls`, run each via `registry.call_tool`, append the results as `role: "tool"` messages, loop. Capped at **5 steps**. | ||
| 4. Otherwise return the assistant content as the final reply. | ||
|
|
||
| Use a tool-capable model (e.g. `qwen3:8b`). Check with `ollama show <model>` for the `tools` capability. | ||
|
|
||
| ## Audit Logs | ||
|
|
||
| Every MCP-relevant event is emitted as a `LogEntry` with `kind = "mcp"` via `state.emit_log`. They flow through the existing SSE log stream (`GET /v1/logs`) and are visible on the dashboard: | ||
|
|
||
| - `loading MCP config…` | ||
| - `filesystem ready (2 tools)` | ||
| - `MCP ready (2 tools)` | ||
| - `tools available: filesystem.read_file, filesystem.list_directory` | ||
| - `tool call (0): filesystem.list_directory({"path":"/tmp"})` | ||
| - `tool result (842 bytes)` | ||
| - `tool error: …` | ||
|
|
||
| That single audit trail is the "auditable protocol" promise of this feature. | ||
|
|
||
| ## Try It | ||
|
|
||
| 1. `npx -y @modelcontextprotocol/server-filesystem /tmp` should run (Node + npm available). | ||
| 2. `ollama pull qwen3:8b` (or any tool-capable model). | ||
| 3. `bun run tauri dev`. On first launch, watch the dashboard for `mcp` lines confirming the filesystem server connected. | ||
| 4. Connect a Telegram bot, then send: *"List the files in /tmp."* | ||
| 5. Expect a `tool call` and `tool result` line in the log, followed by a coherent reply on Telegram. | ||
|
|
||
| ## Future Work | ||
|
|
||
| Permission prompts, multiple servers in the default config, a frontend tools panel, hot reload of `mcp.json`, HTTP/SSE transport, resources & prompts. Not in this PR. | ||
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.