-
Notifications
You must be signed in to change notification settings - Fork 88
Add MCP server for LLM interaction with Goblint #1938
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
Draft
Copilot
wants to merge
10
commits into
master
Choose a base branch
from
copilot/add-mcp-server-for-goblint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
be609b2
Initial plan
Copilot 3f89fc8
Add MCP server implementation for Goblint
Copilot faa34bf
Add examples README for MCP server
Copilot 512ee67
Address code review feedback for MCP server
Copilot 149a1df
Add architecture documentation for MCP server
Copilot b6acc14
Expose advanced query and incremental analysis features in MCP server
Copilot 1356469
Refactor MCP server to call Goblint directly instead of via Server mo…
Copilot 50c7cb7
Fix code review issues in MCP server
Copilot 85f0499
Refactor server utilities to eliminate code duplication
Copilot b7c1c8f
Improve documentation in ServerUtil and McpServer
Copilot 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| # MCP Server Architecture | ||
|
|
||
| This document describes the architecture and implementation details of the Goblint MCP Server. | ||
|
|
||
| ## Overview | ||
|
|
||
| The MCP (Model Context Protocol) server provides a standardized way for LLMs and other clients to interact with Goblint's static analysis capabilities. It implements the MCP specification version 2024-11-05. | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| ┌─────────────────┐ | ||
| │ MCP Client │ (e.g., Claude Desktop, custom tools) | ||
| │ (LLM/Tool) │ | ||
| └────────┬────────┘ | ||
| │ JSON-RPC 2.0 over stdin/stdout | ||
| ▼ | ||
| ┌─────────────────┐ | ||
| │ mcpServer.ml │ MCP Protocol Layer | ||
| │ - Tool defs │ - Handles MCP initialize/tools/call | ||
| │ - Request │ - Converts MCP calls to Goblint ops | ||
| │ handling │ | ||
| └────────┬────────┘ | ||
| │ | ||
| ▼ | ||
| ┌─────────────────┐ | ||
| │ server.ml │ Existing Goblint Server | ||
| │ - analyze() │ - Core analysis infrastructure | ||
| │ - file parsing │ - Incremental analysis support | ||
| │ - query APIs │ | ||
| └────────┬────────┘ | ||
| │ | ||
| ▼ | ||
| ┌─────────────────┐ | ||
| │ Goblint Core │ Analysis Engine | ||
| │ - CIL │ | ||
| │ - Solvers │ | ||
| │ - Analyses │ | ||
| └─────────────────┘ | ||
| ``` | ||
|
|
||
| ## Key Components | ||
|
|
||
| ### mcpServer.ml | ||
|
|
||
| The main MCP server implementation: | ||
|
|
||
| - **Protocol Types**: Defines MCP-specific types (ToolInput, ToolCall, ToolResult) | ||
| - **Tool Definitions**: Specifies available tools with JSON schemas | ||
| - **Request Handlers**: Processes MCP requests and dispatches to Goblint | ||
| - **I/O Loop**: Reads JSON-RPC from stdin, writes to stdout | ||
|
|
||
| ### goblint_mcp_server.ml | ||
|
|
||
| Simple executable entry point that: | ||
| 1. Parses command-line arguments | ||
| 2. Initializes Goblint | ||
| 3. Starts the MCP server loop | ||
|
|
||
| ## Protocol Flow | ||
|
|
||
| 1. **Initialize**: Client sends `initialize` with capabilities | ||
| 2. **Tools Discovery**: Client calls `tools/list` to get available tools | ||
| 3. **Tool Execution**: Client calls `tools/call` with tool name and arguments | ||
| 4. **Response**: Server returns results as MCP ToolResult objects | ||
|
|
||
| ## Tool Implementation Pattern | ||
|
|
||
| Each tool follows this pattern: | ||
|
|
||
| ```ocaml | ||
| | "tool_name" -> | ||
| (* 1. Extract and validate arguments *) | ||
| let arg = Yojson.Safe.Util.member "arg" args |> to_type in | ||
|
|
||
| (* 2. Configure Goblint if needed *) | ||
| GobConfig.set_* "option" value; | ||
|
|
||
| (* 3. Execute operation *) | ||
| Server.analyze mcp_serv.server ~reset; | ||
|
|
||
| (* 4. Return result *) | ||
| ToolResult.make_text "Success message" | ||
| ``` | ||
|
|
||
| ## Configuration Handling | ||
|
|
||
| The `configure` tool converts MCP JSON values to Goblint config format: | ||
| - `string` → `"value"` (with quotes) | ||
| - `bool` → `true` / `false` | ||
| - `int` → `123` | ||
| - `float` → `123.45` | ||
| - Complex types → JSON string | ||
|
|
||
| ## Error Handling | ||
|
|
||
| - JSON parse errors: Logged with input context, server continues | ||
| - Tool errors: Returned as `ToolResult` with `isError: true` | ||
| - Analysis errors: Caught and converted to error results | ||
| - EOF on stdin: Clean shutdown with log message | ||
|
|
||
| ## Future Extensions | ||
|
|
||
| To add new tools: | ||
|
|
||
| 1. Add tool definition to `tools` list with: | ||
| - Name | ||
| - Description | ||
| - Input JSON schema | ||
|
|
||
| 2. Add case to `handle_tools_call`: | ||
| ```ocaml | ||
| | "new_tool" -> | ||
| (* implementation *) | ||
| ToolResult.make_text "result" | ||
| ``` | ||
|
|
||
| 3. Update documentation in `docs/user-guide/mcp-server.md` | ||
|
|
||
| ## Testing | ||
|
|
||
| Use `scripts/mcp-test.py` for basic protocol testing: | ||
| ```bash | ||
| ./scripts/mcp-test.py path/to/test.c | ||
| ``` | ||
|
|
||
| For production testing, integrate with actual MCP clients like Claude Desktop. | ||
|
|
||
| ## Related Modules | ||
|
|
||
| - **Server** (src/util/server.ml): Existing JSON-RPC server infrastructure | ||
| - **Maingoblint**: Entry points for analysis execution | ||
| - **GobConfig**: Configuration management | ||
| - **Messages**: Analysis warning/error collection | ||
|
|
||
| ## Dependencies | ||
|
|
||
| - `jsonrpc` (>= 1.12): JSON-RPC 2.0 protocol implementation | ||
| - `yojson`: JSON parsing and generation | ||
| - `ppx_deriving_yojson`: Automatic JSON serialization | ||
|
|
||
| ## References | ||
|
|
||
| - [MCP Specification](https://spec.modelcontextprotocol.io/) | ||
| - [Goblint Server Documentation](../docs/user-guide/inspecting.md) | ||
| - [JSON-RPC 2.0 Spec](https://www.jsonrpc.org/specification) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems unnecessarily roundabout to convert the MCP JSON-RPC into our custom JSON-RPC just to call some functionality in Goblint.
The MCP server is already being built together with Goblint (which I think makes sense), so it might as well be direct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please address simmo's comment by changing the implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored to remove the indirection. The MCP server now calls Goblint functions directly instead of going through the Server module's JSON-RPC layer. It manages its own state (file, arg_wrapper, invariant_parser, node_locator) and directly calls Maingoblint.do_analyze, GobConfig, Messages.Table, etc. This makes the architecture much cleaner. Commit: 1356469
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now it just copied a bunch of code from
server.ml...I think
server.mlneeds some refactoring to do this without duplication. Currently it mixes the whole Goblint server interface (I/O), the JSON-RPC query management (including various Yojson conversion), interactive analysis state management and the core logic of interacting with the rest of Goblint.A lot of the logic is in unnamed first-class modules whose contents cannot really be reused right now I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot: address simmo's comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored to eliminate duplication. Created a new
ServerUtilmodule that extracts shared functionality (arg_wrapper, invariant_parser, node_locator creation, is_server_node). Both server.ml and mcpServer.ml now use these utilities, removing ~107 lines of duplicated code. The server logic is now properly separated: protocol handling in server.ml/mcpServer.ml, shared utilities in serverUtil.ml, and core Goblint in Maingoblint/GobConfig/etc. Commit: 85f0499