mcp/

Model Context Protocol in both directions: consume remote servers, host your own.

On this page

mcp/

MCP (Model Context Protocol) works two directions - consuming remote servers, and hosting your own.

flowchart LR
    subgraph consume["CONSUMING - declared on Agent.yml via mcpServers"]
        direction LR
        AG["your agent"] -->|"calls out at runtime"| RM["a remote MCP server<br/>https://example.com/mcp"]
    end

    subgraph host["HOSTING - one file per server under mcp/"]
        direction LR
        TL["tools/<br/>your @AITool functions"] -->|"a named SUBSET"| MS["mcpServer( 'localServer' )<br/>registered at onApplicationStart()"]
        MS --> GW["a gateways/ entry with exposes: mcp<br/>route( '/mcp/tools' ).toMCP()"]
        GW -->|"reachable over HTTP"| OC["some outside MCP client"]
    end

    style consume fill:#e7f1ff,stroke:#004085
    style host fill:#eaf6ec,stroke:#155724

Nothing links the two: an agent consuming remote servers need not host one, and a hosted server is reachable only if a gateways/ entry exposes it.

Tip

bxAgents add mcp client github adds a server to Agent.yml with its credential as an environment placeholder, and bxAgents add mcp server writes the mcp/ file and the gateways/ entry that serves it. See add.

Consuming remote servers

Declared directly on Agent.yml (not a file under mcp/), via mcpServers:

# Agent.yml
name: my-agent
model: openai/gpt-5
mcpServers:
  - https://example.com/mcp
  - url: https://other.com/mcp
    name: other

Each entry is either a bare URL string or a mapping. A mapping can carry the credentials the server wants:

mcpServers:
  - name: github
    url: https://api.githubcopilot.com/mcp/
    token: ${env.GITHUB_PERSONAL_ACCESS_TOKEN}
  - url: https://other.com/mcp
    headers:
      X-Api-Key: ${env.OTHER_API_KEY}
    timeout: 20000
KeyMeaning
urlRequired. The server's address
nameA label for the file. It is not sent to the server
tokenA bearer token. Must be an ${env.NAME} placeholder: a token written in the file is a build error
password, userBasic authentication. password must be an ${env.NAME} placeholder
headersExtra request headers. Values can use ${env.NAME}
timeoutMilliseconds
oauthThe server asks each person to sign in (mail, a calendar). oauth: true or a mapping. Needs a name. See connect
allowedHostsThe host names the server's address may have, for example [api.example.com, "*.example.com"]. *.example.com matches any subdomain, not example.com itself. A request to any other host is refused before anything is sent
redirectsfalse treats a redirect answer as a failure and never follows it
maxResponseBytesRefuses an answer longer than this many characters
handshaketrue sends the MCP initialize handshake before the first call
prefixTools are named prefix__tool, so two servers cannot clash
toolFilterA list of tool names; only these are given to the agent

Placeholders are replaced with the environment variable when the app starts, never at build time, so a secret stays out of the project and out of a packaged .bxa. A variable that is not set fails the agent at startup and names it; one that is set but empty lets the agent start, the server refuses the connection, and bx-ai logs that and carries on without that server's tools. Any other key is a build error that lists the valid ones.

The entries are passed into the generated aiAgent(mcpServers: [...]) call. No network connection is ever attempted at build time - reachability is a runtime concern; an unreachable server at build time is not a build error. This section is for servers reached over the web; servers that run as a program on the agent's machine are next.

bxAgents new offers GitHub, Stripe, Notion, Atlassian, Cloudflare, Supabase and Git and writes the entry and the .env stub for you.

Subagents can declare their own mcpServers independently - each node in the agent tree gets its own resolved list.

Servers that run as a program

An entry with command starts an MCP server as a program on the machine that runs the agent, and talks to it over the program's standard input and output:

mcpServers:
  - name: git
    command: [uvx, mcp-server-git]
    prefix: git
    toolFilter: [git_status, git_diff, git_log]
  - name: browser
    command: [bxplaywright, mcp]
    env:
      PLAYWRIGHT_API_KEY: ${env.PLAYWRIGHT_API_KEY}
    cwd: .
KeyMeaning
commandRequired. A list: the program, then its arguments. It is never run through a shell
nameA label for the file
envEnvironment variables for the program. A variable whose name contains KEY, TOKEN, SECRET, PASSWORD or CREDENTIAL must be an ${env.NAME} placeholder
cwdThe folder the program starts in
inheritEnvfalse starts it with only env. Default true
maxRestartsHow many times in a row a program that died is started again. Default 2
timeoutMilliseconds per request
prefix, toolFilterAs for web servers

The program starts on the first call and is closed when the app stops. command cannot be combined with url, and the keys that only make sense for a web server (token, headers, user, password, allowedHosts, redirects, maxResponseBytes, handshake) are a build error on it.

When the app starts, the programs your agents declare become bx-ai's mcp.stdio.allowedCommands setting, so the process starts only what the project declared. A list you set yourself (for example in .boxlang.json) is left alone and must name the programs. bxAgents doctor warns when a program is not on the PATH.

A program runs with your user's rights on the machine that hosts the agent. Read what you start, and use toolFilter to limit what the agent may call.

Hosting a local server

Each mcp/*.bx file is a local MCP server your project hosts, exposing a subset of your tools/ as MCP tools:

// mcp/localServer.bx
class {

	function configure() {
		return {
			description : "Internal tools MCP server",
			version     : "1.0.0",
			cors        : "*",             // optional - CORS origin(s) allowed to call this server; omit for none
			tools       : [ "sayHello" ]   // names of tools already declared under tools/
		};
	}

}

The entry's discovered name is its filename (localServer.bx β†’ localServer), not any name field inside its own configure() struct - a project may still set one for documentation, but it's ignored for naming/registration purposes.

cors is optional and defaults to an empty string (no CORS header) when omitted - passed straight through as mcpServer()'s 4th positional argument.

At build time, the file is copied verbatim into the generated app's mcp/ folder, and a registration statement is emitted into the generated base class (bxagents/Application.bx)'s after(), which runs once ColdBox has loaded:

mcpServer( "localServer", "Internal tools MCP server", "1.0.0", "*" )
	.registerTool( aiToolRegistry().get( "sayHello" ) )

mcpServer(name, ...) is a global, name-keyed singleton getter in bx-ai - registering it once at startup is all that's needed; there's no WireBox mapping involved (unlike the agent-exposure singleton used by toAi()).

Exposing a local server over HTTP

A local mcp/* server isn't reachable over HTTP on its own - pair it with a gateways/ exposure entry naming it as the target:

// gateways/expose-mcp.bx
class {
	function configure() {
		return {
			exposes : "mcp",
			path    : "/mcp/tools",
			target  : "localServer"
		};
	}
}

Validation

  • An mcpServers entry (struct form) needs a url or a command, not both and not neither.
  • An mcpServers entry that's neither a non-empty string nor a struct fails validation.
  • A command must be a list of non-empty words. A key for the other kind of server, an env secret written in the file, and a mistyped allowedHosts, redirects, handshake, prefix or toolFilter fail validation.
  • A gateways/ entry with exposes: "mcp" and a target that doesn't match any discovered mcp/* entry fails validation.
Edit this page Download Markdown Last updated Nov 26, 2026, 8:45:51 AM Report an issue