Skip to content

MCP ​

An MCPClient block connects an agent to an MCP server: a local subprocess, or a server over HTTP. The server's tools are registered when the agent is built, so the model can use them from the first turn.

There are two ways to give an agent MCP tools:

ApproachYAMLWhen to use
Declarative (this page)type: MCPClient blocks, listed under the agent's mcp:The servers are fixed. Tools are ready when the agent is built
Runtime discoverytools: [elsai_tools.mcp_client]The model connects to servers and loads tools itself. Needs elsai-agents-tools

mcp: is a key on the Agent block, next to model, tools, memory, knowledge, and guardrails. It is not part of model.


Installation ​

Declarative MCP needs only elsai-yaml and elsai-agents 0.4.0 or later. elsai-agents ships the MCP client and the transports.

bash
pip install --upgrade --extra-index-url https://elsai-agents.elsai.ai/root/ "elsai-yaml==0.4.3"

For stdio servers started with uvx or npx, those programs must be on PATH when the agent starts. The first run may download the server package.

Install elsai-agents-tools only if you also list elsai_tools.* tools, or use elsai_tools.mcp_client for runtime discovery.


Environment variables ​

Keep tokens and folder paths in the environment, and reference them with placeholders.

PurposeExamples
Chat modelOPENAI_API_KEY, OPENAI_MODEL_ID
Filesystem server rootMCP_FS_ROOT, the folder the server may read
HTTP or SSE serverMCP_SSE_URL, and tokens such as MCP_TOKEN

{{$VAR:}} is required. {{$VAR:default}} falls back to the default. Placeholders are filled when the whole file loads, so a missing required variable fails the file even for agents that do not use that block. Give it a default, or move the block to its own file.


YAML rules ​

  • ${block_name} inserts the whole MCPClient block.
  • Values under args, env, and headers must be strings. Quote numbers, for example "8080".
  • mcp: must be a list. Write a block list, or a quoted flow list such as mcp: ["${fetch_mcp}"]. Unquoted mcp: [${fetch_mcp}] is a YAML parse error.
  • auth, client, transport_callable, and elicitation_callback are rejected. Build MCPClient in Python if you need them.

Complete example ​

This agent can fetch web pages and read files from one folder.

agents.yaml:

yaml
openai_model:
  type: OpenAIModel
  model_id: gpt-4o-mini

fetch_mcp:
  type: MCPClient
  transport: stdio
  command: uvx
  args: [--quiet, mcp-server-fetch]
  startup_timeout: 120
  prefix: web
  tool_filters:
    allowed: [fetch]

files_mcp:
  type: MCPClient
  transport: stdio
  command: npx
  args: [-y, "@modelcontextprotocol/server-filesystem", "{{$MCP_FS_ROOT:}}"]
  startup_timeout: 120
  prefix: files
  tool_filters:
    allowed: [read_text_file, list_directory]

analyst:
  type: Agent
  name: analyst
  system_prompt: Use tools for live web pages and allowed files.
  model: ${openai_model}
  tools: []
  mcp:
    - ${fetch_mcp}
    - ${files_mcp}
bash
export OPENAI_API_KEY=...
export MCP_FS_ROOT=$HOME/reports
python
from elsai.config import yaml_to_agent

agent = yaml_to_agent("agents.yaml", entry="analyst")
try:
    print(agent("Summarize https://example.com using your fetch tool."))
    print(agent("List the files you can read, then summarise the newest one."))
finally:
    agent.cleanup()

The model sees web_fetch, files_read_text_file, and files_list_directory: the prefix, an underscore, then the server's tool name.

Call agent.cleanup() when you are done. It stops the subprocesses and closes the HTTP sessions.


Mixing MCP with other tools ​

MCP tools are added after the tools list. A server block can also be written inline under mcp:.

yaml
analyst:
  type: Agent
  model: ${openai_model}
  system_prompt: Use the tools for arithmetic and live data.
  tools:
    - elsai_tools.calculator
  mcp:
    - ${fetch_mcp}
    - type: MCPClient
      transport: streamable_http
      server_url: https://mcp.example.com/mcp
      prefix: api
      headers:
        Authorization: "Bearer {{$MCP_TOKEN:}}"

Build-time rules:

  • The same named MCP block cannot appear twice on one agent.
  • Prefixes must be unique on an agent. my-srv and my_srv collide.
  • Two tools with the same name, from two servers or from tools, fail the build. Use prefix or tool_filters to separate them.

If the build fails after some servers have started, the loader closes them before raising the error.


One client without an agent ​

yaml_to_mcp builds a single MCPClient from a block, without starting it. Pass it to an agent you build in Python.

python
from elsai import Agent
from elsai.config import yaml_to_mcp
from elsai.models.openai import OpenAIModel

fetch = yaml_to_mcp("agents.yaml", entry="fetch_mcp")
agent = Agent(model=OpenAIModel(model_id="gpt-4o-mini"), tools=[fetch])
try:
    print(agent("Fetch https://example.com and give me the title."))
finally:
    agent.cleanup()

The server starts when the agent loads its tools, the same as with mcp:.


Transports ​

transportRequiredOptional
stdio (default)commandargs, env, cwd
sseserver_url (http or https)headers, timeout, sse_read_timeout
streamable_httpserver_url (http or https)headers, timeout, sse_read_timeout, terminate_on_close

Every transport also accepts startup_timeout, prefix, tool_filters, use_cache, cache_dir, and tasks_config (ttl and poll_timeout, in seconds). A key that belongs to another transport, such as server_url on stdio, is rejected.

stdio ​

The server runs as a subprocess. This is the usual choice for community servers run with uvx or npx.

yaml
fetch_mcp:
  type: MCPClient
  transport: stdio
  command: uvx
  args: [--quiet, mcp-server-fetch]
  env:
    SOME_FLAG: "1"

env is added to a minimal safe environment. The parent process environment is not copied, so list every variable the server needs.

sse and streamable_http ​

The server runs elsewhere and is reached over HTTP.

yaml
remote_mcp:
  type: MCPClient
  transport: streamable_http
  server_url: https://mcp.example.com/mcp
  timeout: 30
  headers:
    Authorization: "Bearer {{$MCP_TOKEN:}}"
  prefix: api

Many hosts now serve streamable HTTP at /mcp instead of a legacy SSE URL. Use the URL your server documents.


Prefixes and tool filters ​

KeyWhat it does
prefixTool names become {prefix}_{tool_name}, so servers do not clash. 1 to 62 letters, digits, _, or -. Unique per agent
tool_filters.allowedKeep only these server tool names, written without the prefix
tool_filters.rejectedDrop these server tool names

Tool authorization sees the prefixed name. Write web_fetch in allowed_tools or denied_tools, not fetch.


Tool cache ​

In YAML, use_cache defaults to false. The Python MCPClient defaults to true. Turn it on only for a stable server whose tools fit in a single page:

yaml
fetch_mcp:
  type: MCPClient
  transport: stdio
  command: uvx
  args: [--quiet, mcp-server-fetch]
  use_cache: true
  cache_dir: /var/cache/mcp-tools

With memory, RAG, and guardrails ​

MCP sits on the same Agent block as the other features:

yaml
support:
  type: Agent
  model: ${openai_model}
  system_prompt: Answer support questions. Fetch pages when the user sends a link.
  tools: []
  memory: ${session_memory}
  knowledge:
    base: ${policy_kb}
    mode: basic
  guardrails: ${content_rails}
  mcp:
    - ${fetch_mcp}

See Memory, RAG, and Guardrails for those blocks. If the knowledge base fails to attach after the servers start, the servers are still closed.


What YAML does not cover ​

Declarative MCP registers tools only. MCP resources and prompt templates are not wired. Use the MCPClient methods in Python for those.


Common mistakes ​

ProblemWhat to do
Tool name '…' already existsAdd prefix or tool_filters. Two servers, or a server and tools, have the same tool name
mcp: [${ref}] parse errorQuote it, mcp: ["${ref}"], or use a block list
Key rejected for the transportstdio takes command; sse and streamable_http take server_url
Secret in the YAML fileUse {{$TOKEN:}} in env or headers
path outside allowed directoriesThe filesystem server only reads under MCP_FS_ROOT
SSE returns 410 GoneThe server moved to streamable HTTP. Update server_url and transport
JSON noise from uvxPut --quiet before the package name in args
Subprocesses left runningCall agent.cleanup(), especially in long-running apps

Copyright © 2026 elsai foundry.