mcptools: Model Context Protocol for R

repository·main·Indexed 41 days ago

https://github.com/posit-dev/mcptools

mcptools implements the Model Context Protocol (MCP) in R, enabling bidirectional communication between AI coding assistants and R sessions. It allows R to function as an MCP server, enabling AI to run R code and inspect environments, or as an MCP client to integrate third-party tools into ellmer chats. It supports configuration for Claude Desktop, Claude Code, and deployment to Posit Connect.

Tokens
746
Snippets
6
Records
6
Agent score
3%

What's inside mcptools

  1. Use R as an MCP client to integrate third-party tools

    main

    You can use mcptools to register third-party MCP servers (using the Claude Desktop configuration format) into ellmer chats.

    By default, mcptools looks for a configuration file at file.path("~", ".config", "mcptools", "config.json"). Once configured, use mcp_tools() to retrieve the tools and pass them to an ellmer chat object via $set_tools().

    # Example: Integrating GitHub MCP server into an ellmer chat
    ch <- ellmer::chat_anthropic()
    ch$set_tools(mcp_tools())
    
    ch$chat("What issues are open on posit-dev/mcptools?")
  2. Deploy an HTTP MCP server to Posit Connect

    main

    To deploy an MCP server to Posit Connect, follow these steps:

    1. Create a _server.yml file in your project directory:
      engine: mcptools
      tools: tools.R
    2. Deploy the directory as an R API and mark it as MCP content using rsconnect:
      rsconnect::deployAPI(".", contentCategory = "mcp")
    3. If the content URL is https://connect.example.com/content/abc123/, your MCP endpoint will be https://connect.example.com/content/abc123/mcp.

    Note: If you cannot set contentCategory = "mcp" during deployment, set the MCP category in Connect manually after deployment and ensure minimum processes are set to at least 1.

    engine: mcptools
    tools: tools.R
    rsconnect::deployAPI(".", contentCategory = "mcp")
  3. Configure R as an MCP server for Claude Desktop

    main

    To allow Claude Desktop to run R code in your sessions, add the r-mcptools server to your Claude Desktop configuration file.

    On macOS, this file is located at ~/Library/Application Support/Claude/claude_desktop_config.json.

    {
      "mcpServers": {
        "r-mcptools": {
          "command": "Rscript",
          "args": ["-e", "mcptools::mcp_server()"]
        }
      }
    }
  4. Expose specific R sessions to MCP models

    main

    By default, the MCP server provides tools for R. To allow models to access variables and objects in specific R sessions, call mcptools::mcp_session() within those sessions.

    Tip: You can add mcptools::mcp_session() to your .Rprofile (using usethis::edit_r_profile()) to automatically register every R session you start.

    mcptools::mcp_session()