The mcpr package allows you to not only create Model
Context Protocol (MCP) servers in R but also connect to existing MCP
servers as a client. This vignette will guide you through using the MCP
client to interact with servers.
The client functionality allows you to connect to any MCP server, whether it’s running locally as a separate process, or remotely via HTTP. Here’s how to create a client:
You can connect to a server running as a separate process. This is useful for testing or when you need to communicate with a local MCP implementation:
Once you have created a client, you can interact with the server using the various client methods:
It’s good practice to initialize the connection first:
You can list all the tools that the server provides:
Once you know what tools are available, you can call them with specific parameters:
If the server has prompts available, you can list and use them:
Here’s a complete example that demonstrates connecting to a calculator server and performing operations:
# Create a client
client <- new_client(
"Rscript",
system.file("examples/calculator/server.R", package = "mcpr"),
name = "calculator",
version = "1.0.0"
)
# Initialize the connection
initialize(client)
# List available tools
tools <- tools_list(client)
print(tools)
# Call the math_calculator tool with different operations
add_result <- tools_call(
client,
name = "math_calculator",
params = list(
operation = "add",
a = 10,
b = 5
)
)
print(add_result)
multiply_result <- tools_call(
client,
name = "math_calculator",
params = list(
operation = "multiply",
a = 10,
b = 5
)
)
print(multiply_result)MCP clients can be used in various scenarios:
The MCP client functionality in the mcpr package
provides a flexible way to interact with MCP servers from R. Whether
you’re building applications that consume AI capabilities or creating
distributed systems that communicate via MCP, the client interface makes
it easy to work with the protocol.
For more advanced usage, check the function documentation and other vignettes.