-
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 2 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,305 @@ | ||
| # MCP Server for Goblint | ||
|
|
||
| This document describes the Model Context Protocol (MCP) server for Goblint, which allows Large Language Models (LLMs) and other MCP-compatible clients to interact with Goblint's static analysis capabilities. | ||
|
|
||
| ## Overview | ||
|
|
||
| The MCP server provides a standardized JSON-RPC 2.0 based interface for: | ||
| - Configuring Goblint analysis options | ||
| - Running analysis on single C files | ||
| - Running analysis on projects using compilation databases | ||
| - Querying analysis results | ||
|
|
||
| ## Installation | ||
|
|
||
| Build and install the MCP server: | ||
|
|
||
| ```bash | ||
| make | ||
| make install | ||
| ``` | ||
|
|
||
| This will install the `goblint-mcp-server` executable into your opam switch. | ||
|
|
||
| ## Usage | ||
|
|
||
| The MCP server communicates via stdin/stdout using JSON-RPC 2.0: | ||
|
|
||
| ```bash | ||
| goblint-mcp-server | ||
| ``` | ||
|
|
||
| The server follows the MCP protocol specification and can be integrated with any MCP-compatible client or LLM framework. | ||
|
|
||
| ## MCP Protocol | ||
|
|
||
| ### Initialization | ||
|
|
||
| The client should initialize the connection with: | ||
|
|
||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": 1, | ||
| "method": "initialize", | ||
| "params": { | ||
| "protocolVersion": "2024-11-05", | ||
| "capabilities": {}, | ||
| "clientInfo": { | ||
| "name": "your-client-name", | ||
| "version": "1.0.0" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Available Tools | ||
|
|
||
| The server provides the following tools (accessible via `tools/list` and `tools/call`): | ||
|
|
||
| #### 1. configure | ||
|
|
||
| Configure Goblint analysis options. | ||
|
|
||
| **Input Schema:** | ||
| ```json | ||
| { | ||
| "option": "string", | ||
| "value": "string" | ||
| } | ||
| ``` | ||
|
|
||
| **Example:** | ||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": 2, | ||
| "method": "tools/call", | ||
| "params": { | ||
| "name": "configure", | ||
| "arguments": { | ||
| "option": "ana.activated[+]", | ||
| "value": "'base'" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### 2. reset_config | ||
|
|
||
| Reset all configuration to default values. | ||
|
|
||
| **Input Schema:** | ||
| ```json | ||
| {} | ||
| ``` | ||
|
|
||
| #### 3. analyze_file | ||
|
|
||
| Run Goblint analysis on a single C source file. | ||
|
|
||
| **Input Schema:** | ||
| ```json | ||
| { | ||
| "file": "string", | ||
| "reset": "boolean (optional)" | ||
| } | ||
| ``` | ||
|
|
||
| **Example:** | ||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": 3, | ||
| "method": "tools/call", | ||
| "params": { | ||
| "name": "analyze_file", | ||
| "arguments": { | ||
| "file": "/path/to/source.c", | ||
| "reset": false | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### 4. analyze_project | ||
|
|
||
| Run Goblint analysis on a project using a compilation database. | ||
|
|
||
| **Input Schema:** | ||
| ```json | ||
| { | ||
| "compilation_database": "string", | ||
| "reset": "boolean (optional)" | ||
| } | ||
| ``` | ||
|
|
||
| **Example:** | ||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": 4, | ||
| "method": "tools/call", | ||
| "params": { | ||
| "name": "analyze_project", | ||
| "arguments": { | ||
| "compilation_database": "/path/to/compile_commands.json", | ||
| "reset": false | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### 5. get_messages | ||
|
|
||
| Retrieve all analysis messages (warnings, errors, etc.) from the last analysis run. | ||
|
|
||
| **Input Schema:** | ||
| ```json | ||
| {} | ||
| ``` | ||
|
|
||
| **Returns:** JSON array of message objects with location, severity, and text. | ||
|
|
||
| #### 6. get_functions | ||
|
|
||
| Get a list of all functions found in the analyzed code. | ||
|
|
||
| **Input Schema:** | ||
| ```json | ||
| {} | ||
| ``` | ||
|
|
||
| **Returns:** JSON array of function objects with name and location. | ||
|
|
||
| #### 7. query_state | ||
|
|
||
| Query the analysis state at a specific program location. | ||
|
|
||
| **Input Schema:** | ||
| ```json | ||
| { | ||
| "node": "string" | ||
| } | ||
| ``` | ||
|
|
||
| **Example:** | ||
| ```json | ||
| { | ||
| "jsonrpc": "2.0", | ||
| "id": 5, | ||
| "method": "tools/call", | ||
| "params": { | ||
| "name": "query_state", | ||
| "arguments": { | ||
| "node": "main:1" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Integration with LLM Frameworks | ||
|
|
||
| ### Claude Desktop | ||
|
|
||
| Add to your Claude Desktop configuration (`~/Library/Application Support/Claude/claude_desktop_config.json` on macOS): | ||
|
|
||
| ```json | ||
| { | ||
| "mcpServers": { | ||
| "goblint": { | ||
| "command": "goblint-mcp-server", | ||
| "args": [] | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Other MCP Clients | ||
|
|
||
| The server is compatible with any MCP client that follows the Model Context Protocol specification. Refer to your client's documentation for integration instructions. | ||
|
|
||
| ## Example Workflow | ||
|
|
||
| 1. Initialize the connection | ||
| 2. (Optional) Configure analysis options using `configure` | ||
| 3. Run analysis using either `analyze_file` or `analyze_project` | ||
| 4. Query results using `get_messages`, `get_functions`, or `query_state` | ||
| 5. (Optional) Analyze another file or project | ||
|
|
||
| ## Advanced Usage | ||
|
|
||
| ### Compilation Databases | ||
|
|
||
| For complex projects, use a compilation database (compile_commands.json) which can be generated by build systems like CMake or using tools like Bear: | ||
|
|
||
| ```bash | ||
| # Using Bear to capture compilation commands | ||
| bear -- make | ||
|
|
||
| # This generates compile_commands.json | ||
| goblint-mcp-server | ||
| # Then use the analyze_project tool with the path to compile_commands.json | ||
| ``` | ||
|
|
||
| ### Configuration Options | ||
|
|
||
| Goblint has many configuration options. Common options include: | ||
|
|
||
| - `ana.activated[+]`: Add an analysis (e.g., `'base'`, `'threadid'`, `'race'`) | ||
| - `dbg.level`: Set debug level (`"info"`, `"debug"`, `"verbose"`) | ||
| - `pre.includes[+]`: Add include directory | ||
| - `ana.sv-comp.functions`: Enable SV-COMP mode | ||
|
|
||
| See the [Goblint documentation](https://goblint.readthedocs.io/) for a complete list of options. | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Server Not Responding | ||
|
|
||
| Ensure the server is receiving properly formatted JSON-RPC 2.0 messages. Each message should be a single line terminated with a newline. | ||
|
|
||
| ### Analysis Errors | ||
|
|
||
| Check the tool result's `isError` field and `content` for error messages. Common issues: | ||
| - File not found | ||
| - Invalid C syntax | ||
| - Missing include files | ||
| - Invalid configuration options | ||
|
|
||
| ### Debug Mode | ||
|
|
||
| To enable debug logging, configure Goblint before analyzing: | ||
|
|
||
| ```json | ||
| { | ||
| "method": "tools/call", | ||
| "params": { | ||
| "name": "configure", | ||
| "arguments": { | ||
| "option": "dbg.level", | ||
| "value": "\"debug\"" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Limitations | ||
|
|
||
| - The MCP server currently supports a subset of Goblint's full functionality | ||
| - Complex queries and incremental analysis features from the standard Goblint server mode are not yet exposed via MCP | ||
| - Large projects may take significant time to analyze | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Goblint Documentation](https://goblint.readthedocs.io/) | ||
| - [Model Context Protocol Specification](https://modelcontextprotocol.io/) | ||
| - [Goblint Server Mode Documentation](../user-guide/inspecting.md) | ||
|
|
||
| ## Contributing | ||
|
|
||
| To add new MCP tools or improve the server, modify: | ||
| - `src/util/mcpServer.ml` - Core MCP server implementation | ||
| - `src/goblint_mcp_server.ml` - Executable entry point | ||
|
|
||
| Follow the existing tool patterns and ensure new tools are documented in this file. | ||
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.
@copilot they should be exposed
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.
Done! I've exposed all the complex query and incremental analysis features from the standard Goblint server. The MCP server now provides 17 tools total (up from 7), including:
merge_config,read_configget_files,get_varinfosget_cfg,cfg_lookuparg_lookup,arg_eval,arg_eval_intglobal_stateAll features from the standard server JSON-RPC API are now accessible via MCP. Commit: b6acc14