Nim Model Context Protocol (MCP) SDK

February 24, 2025 ยท View on GitHub

Nim implementation of the Model Context Protocol (MCP) for building AI agent capabilities.

Installation

  1. Install Nim compiler
  2. Clone repository:
git clone https://github.com/yourusername/mcp-nim.git
cd mcp-nim

Configuration

Claude Integration

  1. Locate MCP config file:

    • Claude Desktop: ~/Library/Application Support/Claude/claude_desktop_config.json (macOS)
    • Claude Desktop: C:\Users\xxx\AppData\Roaming\Claude\claude_desktop_config.json (Windows)
    • Cursor: %APPDATA%\Cursor\User\globalStorage\rooveterinaryinc.roo-cline\settings\cline_mcp_settings.json (Windows)
  2. Add server configuration:

{
  "mcpServers": {
    "filesystem-server": {
      "command": "nim",
      "args": ["c","-r", "examples/filesystem_server.nim"],
      "cwd": "/path/to/sdk/root",
      "disabled": false
    }
  }
}

Development

Creating MCP Servers

import std/[asyncdispatch, json, options]
import ../src/mcp/[client, types]
import ../src/mcp/shared/[protocol, stdio, transport]

proc main() {.async.} =
  let transport = newStdioTransport()
  let protocol = newProtocol(some(ProtocolOptions()))
  
  protocol.setRequestHandler(Request, proc(
    request: Request,
    extra: RequestHandlerExtra
  ): Future[McpResult] {.async.} =
    if request.`method` == "your-method":
      result = McpResult()
    else:
      raise newMcpError(ErrorCode.MethodNotFound)
  )

  await protocol.connect(transport)
  await sleepAsync(1000)
  await protocol.close()

when isMainModule:
  waitFor main()

Examples

Filesystem Server

protocol.setRequestHandler("tools/list", proc(request: base_types.JsonRpcRequest, extra: protocol.RequestHandlerExtra): Future[JsonNode] {.async.} =
  result = %*{
    "tools": [
      {
        "name": "read_file",
        "description": "Read file contents",
        "inputSchema": {
          "type": "object",
          "properties": {
            "path": {"type": "string"}
          },
          "required": ["path"],
          "additionalProperties": false
        }
      }
    ]
  }
)

Features

  • Type-safe RPC communication
  • JSON serialization/deserialization
  • Async I/O with progress notifications
  • Capability-based security model
  • Thread-safe implementations

Building

nimble build
nimble test
nimble docs

License

MIT License