Skip to main content

MCP servers

MCP is an open protocol for offering tools to a model. Servers exist for files, GitHub, browsers, databases and much more. The freyja-mcp crate connects to one and hands its tools to an agent, so the agent uses them with no tool code of your own.

[dependencies]
freyja-mcp = "0.7"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

freyja-mcp is built on rmcp, the official MCP SDK for Rust. MCP runs child processes and background tasks, so it needs tokio, unlike freyja itself.

A local server​

Mcp::spawn starts a server as a child process and talks to it over its standard input and output. tools() asks the server what it offers and returns each tool ready for Agent::tools:

use freyja::{Agent, Client, EndpointPreset};
use freyja_mcp::Mcp;

let files = Mcp::spawn("npx", ["-y", "@modelcontextprotocol/server-filesystem", "/srv/docs"]).await?;

let agent = Agent::new(Client::from_env(EndpointPreset::OpenAi)?)
.tools(files.tools().await?);

let run = agent.ask("What does the onboarding doc say about laptops?").await?;

The server's standard error goes to this process's, which is where servers write their logs.

A remote server​

Mcp::connect reaches a server over Streamable HTTP, MCP's transport for servers on the network. bearer sends a token as Authorization: Bearer:

let crm = Mcp::connect("https://mcp.example.com/mcp")
.bearer(std::env::var("CRM_TOKEN")?)
.await?;

OAuth login is not built. A server that needs it is reached with a token you obtained elsewhere.

Several servers​

Two servers can both have a tool called search, and an agent cannot hold two tools of one name. prefix names a server's tools prefix_tool:

let agent = Agent::new(client)
.tools(files.prefix("files").tools().await?) // files_read_file, files_search
.tools(crm.prefix("crm").tools().await?); // crm_search, crm_create_lead

A tool name may hold only letters, digits, _ and -, at most 64 of them, on every provider. MCP allows more, so other characters become _, and a name that is still too long, or that two tools end up sharing, makes tools() return an error naming the tool.

What the model sees​

Each MCP tool becomes a ToolDefinition with the server's name, description and input schema. Strict mode stays off, because MCP schemas rarely meet its rules.

The server returnsThe model reads
TextThe text, parts joined by line breaks
Structured content and no textThe JSON
An image, audio or a resourceA short note, such as [image: image/png]
An error resultThe error text, as a failed tool call it can recover from
Nothing, because the server is gonemcp server ... failed, as a failed tool call

Every call passes through the agent like any other tool, so guards, hooks, Budget and Cancel apply to MCP tools too. A guard that asks a person before a server deletes a file needs no change for MCP.

The connection​

Mcp is one connection, and clones share it. It closes once the last clone and the last of its tools are dropped, and a spawned server's process ends with it. tools() lists the tools when it is called. A server that changes its tools later needs tools() again.

Only tools are supported. MCP's resources, prompts and sampling are not.

Testing​

Mcp::with_transport connects over any rmcp transport, such as an in-memory pipe to a server written in the test with rmcp's server feature. That runs with no process and no network. The testing guide covers scripting the agent's model, which pairs with it to test an agent's use of a server end to end.

See also​

  • Agents, for tools in general
  • Tool, for writing a tool of your own