Learn R Programming

aisdk (version 1.1.0)

create_mcp_client: Create an MCP Client

Description

Convenience function to create and connect to an MCP server.

Usage

create_mcp_client(command, args = character(), env = NULL)

Value

An McpClient object

Arguments

command

The command to run the MCP server

args

Command arguments

env

Environment variables

Examples

Run this code
# \donttest{
if (interactive()) {
  # Connect to GitHub MCP server
  client <- create_mcp_client(
    "npx",
    c("-y", "@modelcontextprotocol/server-github"),
    env = c(GITHUB_PERSONAL_ACCESS_TOKEN = Sys.getenv("GITHUB_TOKEN"))
  )

  # List available tools
  tools <- client$list_tools()

  # Use tools with generate_text
  result <- generate_text(
    model = "openai:gpt-4o",
    prompt = "List my GitHub repos",
    tools = client$as_sdk_tools()
  )

  client$close()
}
# }

Run the code above in your browser using DataLab