Skip to content
146 changes: 146 additions & 0 deletions docs/developer-guide/mcp-architecture.md
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 │
└─────────────────┘
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

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.ml needs 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.

Copy link
Member

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

Copy link
Contributor Author

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 ServerUtil module 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

```

## 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)
Loading