Skip to contents

R6 class representing a Model Context Protocol server. This class manages tools, resources, prompts, and handles the protocol communication.

Public fields

name

Server name

version

Server version

tools

List of registered tools

resources

List of registered resources

prompts

List of registered prompts

Methods


Method new()

Create a new MCP server

Usage

MCPServer$new(name = NULL, version = NULL)

Arguments

name

Server name

version

Server version


Method mcp_tool()

Register a tool with the server

Usage

MCPServer$mcp_tool(name, fn, description = NULL, parameters = NULL)

Arguments

name

Tool name

fn

Function to execute

description

Tool description

parameters

Parameter schema (optional)

Returns

Self for chaining


Method mcp_resource()

Register a resource with the server

Usage

MCPServer$mcp_resource(name, fn, description = NULL, mime_type = "text/plain")

Arguments

name

Resource name

fn

Function that returns resource content

description

Resource description

mime_type

MIME type of the resource

Returns

Self for chaining


Method mcp_prompt()

Register a prompt template with the server

Usage

MCPServer$mcp_prompt(name, template, description = NULL, parameters = NULL)

Arguments

name

Prompt name

template

Prompt template string

description

Prompt description

parameters

List of parameter definitions

Returns

Self for chaining


Method mcp_source()

Load tools/resources from an R source file

Usage

MCPServer$mcp_source(file)

Arguments

file

Path to R file with decorated functions

Returns

Self for chaining


Method mcp_package()

Expose functions from an R package

Usage

MCPServer$mcp_package(package, include = NULL, exclude = NULL)

Arguments

package

Package name

include

Character vector of patterns to include

exclude

Character vector of patterns to exclude

Returns

Self for chaining


Method mcp_run()

Run the MCP server

Usage

MCPServer$mcp_run(transport = "http", host = "127.0.0.1", port = NULL)

Arguments

transport

Transport type ("http", "websocket")

host

Host address for HTTP/WebSocket transport

port

Port number for HTTP/WebSocket transport

Returns

Server handle (transport-specific)


Method generate()

Generate a standalone MCP server package

Usage

MCPServer$generate(path = ".", template = "full", overwrite = FALSE)

Arguments

path

Directory to create the server in

template

Template to use ("full" or "minimal")

overwrite

Whether to overwrite existing directory

Returns

Path to generated server directory


Method get_capabilities()

Get server capabilities for initialization

Usage

MCPServer$get_capabilities()

Returns

List of server capabilities


Method execute_tool()

Execute a tool

Usage

MCPServer$execute_tool(name, arguments = list())

Arguments

name

Tool name

arguments

Tool arguments

Returns

Tool result


Method get_resource()

Get a resource

Usage

MCPServer$get_resource(name)

Arguments

name

Resource name

Returns

Resource content


Method print()

Print method for MCP server

Usage

MCPServer$print(...)

Arguments

...

Additional arguments (ignored) Extract parameters from function signature


Method clone()

The objects of this class are cloneable with this method.

Usage

MCPServer$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.