# Understanding the Model Context Protocol (MCP) in ChocolateLMLite

> Discover the Model Context Protocol MCP in ChocolateLMLite. Learn how this integration layer allows LLMs to dynamically discover and invoke external tools via JSON-RPC at runtime.

- Repository: [Segment (gpsnmeajp)/chocolatelmlite](https://github.com/gpsnmeajp/chocolatelmlite)
- Tags: deep-dive
- Published: 2026-03-02

---

**The Model Context Protocol (MCP) in ChocolateLMLite is a built-in integration layer that enables Large Language Models to dynamically discover and invoke external tools at runtime through standardized JSON-RPC communication with MCP servers.**

ChocolateLMLite, an open-source LLM interface developed by gpsnmeajp, implements the Cursor-style MCP specification to extend its capabilities beyond native functions. This protocol allows the application to treat external processes and HTTP services as first-class tools that the AI can call autonomously during conversations.

## What Is the Model Context Protocol?

The **Model Context Protocol (MCP)** is a standardized specification for connecting AI assistants to external data sources and tools. In ChocolateLMLite, MCP serves as a bridge between the LLM and external services, allowing the model to execute real-world actions through **MCP servers**—dedicated processes that advertise available capabilities via JSON-defined schemas.

When enabled, ChocolateLMLite discovers tools exposed by these servers, converts them into `AITool` objects using the **ModelContextProtocol.Client** library, and adds them to the LLM's available function registry.

## Configuring MCP in ChocolateLMLite

### The Configuration File (data/mcp.json)

ChocolateLMLite reads MCP server definitions from [`data/mcp.json`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/data/mcp.json) at startup. This file maps server names to their connection parameters, supporting both local process and remote HTTP transports.

```json
{
  "McpServers": {
    "RandomNumberServer": {
      "command": "dotnet",
      "args": [ "run", "--project", "RandomTool/RandomTool.csproj" ],
      "workingDirectory": "tools/RandomTool",
      "env": { "RANDOM_SEED": "12345" }
    },
    "HttpEcho": {
      "url": "http://localhost:5000/mcp",
      "headers": { "Authorization": "Bearer abcdef123456" }
    }
  }
}

```

The JSON structure corresponds to the `McpConfig` and `McpServerConfig` classes defined in [`src/Tools.cs`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/src/Tools.cs), which parse these definitions into runtime configuration objects.

### Enabling MCP via the UI

MCP functionality is controlled by the **EnableMcpTools** flag in the system settings. According to [`static/js/system.js`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/static/js/system.js), this toggle activates the MCP initialization sequence when set to `true`.

```json
{
  "EnableMcpTools": true
}

```

When enabled, `Tools.InitToolsAsync()` executes `InitMcpToolsAsync()` to establish connections with configured servers. Due to security implications, this feature defaults to disabled and displays a warning in the interface (line 45 of [`static/js/system.js`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/static/js/system.js)) indicating that MCP tools execute without user confirmation.

## How ChocolateLMLite Implements MCP

### Transport Types

ChocolateLMLite supports two transport mechanisms for MCP communication, both provided by the `ModelContextProtocol.Client` namespace:

- **Stdio Transport**: Launches local commands as child processes using `StdioClientTransport`, suitable for CLI-based tools running on the same machine.
- **HTTP Transport**: Connects to remote services via `HttpClientTransport`, enabling integration with web-based MCP endpoints.

### Tool Discovery Process

The initialization flow in [`src/Tools.cs`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/src/Tools.cs) handles the discovery and registration of MCP tools:

1. **Client Creation**: For each server in [`mcp.json`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/mcp.json), ChocolateLMLite instantiates an appropriate transport client.
2. **Capability Listing**: The system calls `client.ListToolsAsync()` to retrieve available functions from the server.
3. **Registration**: Returned `AITool` objects are stored in the `_mcpTools` collection and merged into `GetAvailableTools()`.

You can inspect loaded tools programmatically:

```csharp
var tools = await toolsInstance.GetAvailableTools();
foreach (var t in tools)
{
    Console.WriteLine($"{t.Name}: {t.Description}");
}

```

This list includes both native ChocolateLMLite functions and dynamically loaded MCP capabilities, as evidenced by the `MCPツール` logging statements in [`Tools.cs`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/Tools.cs).

### Runtime Execution Flow

Once initialized, MCP tools behave identically to native functions. When the LLM decides to invoke an external tool:

1. The model generates a JSON-RPC request targeting the MCP server.
2. ChocolateLMLite routes the request through the established transport (stdio or HTTP).
3. The server executes the requested operation and returns results.
4. The response is injected back into the conversation context for further reasoning.

Example prompt that triggers MCP tool usage:

```csharp
await llm.ChatAsync(@"
You have a tool called get_random().
Please call it to obtain a number and then reply with:
'Your lucky number is {result}'.
");

```

## Security Considerations

MCP tools in ChocolateLMLite execute **without user confirmation**, creating potential security risks if malicious or unstable servers are configured. The codebase explicitly disables this feature by default and requires manual activation through `EnableMcpTools`. Users should only configure MCP servers from trusted sources and verify that [`data/mcp.json`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/data/mcp.json) contains only intended endpoints before enabling the functionality.

## Summary

- **Model Context Protocol (MCP)** in ChocolateLMLite provides standardized integration with external tools through JSON-RPC communication.
- Configuration resides in [`data/mcp.json`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/data/mcp.json) and supports both **stdio** (local processes) and **HTTP** (remote services) transports.
- The [`Tools.cs`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/Tools.cs) file handles initialization via `InitMcpToolsAsync()`, discovers tools through `ListToolsAsync()`, and merges them into the available tool list via `GetAvailableTools()`.
- Users enable the feature through the **EnableMcpTools** flag in [`static/js/system.js`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/static/js/system.js), though it remains disabled by default due to automatic execution risks.
- MCP servers become first-class citizens in the LLM's tool ecosystem, enabling dynamic extension of the assistant's capabilities.

## Frequently Asked Questions

### What transport protocols does ChocolateLMLite support for MCP?

ChocolateLMLite supports two transport types as implemented in [`src/Tools.cs`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/src/Tools.cs): **Stdio** for local command execution using `StdioClientTransport`, and **HTTP** for remote services using `HttpClientTransport`. Both are part of the `ModelContextProtocol.Client` library and are configured in [`data/mcp.json`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/data/mcp.json) using either the `command` key (for stdio) or the `url` key (for HTTP).

### How do I enable MCP tools in the ChocolateLMLite interface?

Set the **EnableMcpTools** flag to `true` in your system settings. According to [`static/js/system.js`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/static/js/system.js), this toggle appears in the UI configuration panel and triggers `Tools.InitToolsAsync()` to run the MCP initialization logic. Note that the interface displays a security warning because MCP tools execute automatically without confirmation prompts.

### Where is the MCP configuration file located?

The configuration file is located at [`data/mcp.json`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/data/mcp.json) relative to the application root. This JSON file defines the `McpServers` object containing server configurations that map to the `McpConfig` and `McpServerConfig` classes in [`src/Tools.cs`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/src/Tools.cs). Each entry specifies connection parameters including command arguments for stdio servers or URLs and headers for HTTP endpoints.

### Are MCP tools safe to use with ChocolateLMLite?

MCP tools execute **without user confirmation** once enabled, making them potentially unsafe if connecting to untrusted servers. The feature is disabled by default in ChocolateLMLite, and the UI explicitly warns users about automatic execution behavior. Only configure servers from trusted sources in [`data/mcp.json`](https://github.com/gpsnmeajp/chocolatelmlite/blob/main/data/mcp.json) and verify all entries before toggling **EnableMcpTools** to `true`.