Exposing Your Agent over HTTP and MCP

gateways/ entries with an exposes key turn your agent into a real HTTP route.

On this page

Exposing Your Agent over HTTP and MCP

gateways/*.bx files cover two distinct, unrelated things - which kind an entry is depends entirely on whether its configure() struct has an exposes key. This lesson covers exposure (exposes: "agent" | "mcp" | "webui"); the next lesson covers the other kind, channel adapters that connect to chat platforms.

flowchart TD
    F["a file under gateways/"] --> Q{"does configure() return<br/>an 'exposes' key?"}
    Q -->|"yes"| E["EXPOSURE<br/>a route into your agent"]
    Q -->|"no - it has 'type' instead"| C["CHANNEL ADAPTER<br/>Connecting Chat Platforms"]

    style E fill:#d4edda,stroke:#155724

Exposing the agent itself

// gateways/expose.bx
class {
	function configure() {
		return {
			exposes : "agent",
			path    : "/api/chat"
		};
	}
}

Generates, in config/Router.bx, four routes served by the agent API's handler: POST /api/chat/invoke, POST /api/chat/stream (SSE), POST /api/chat/batch, GET /api/chat/info. The bare /api/chat path itself is not routable. This is a plain entry point under a path of your choosing; with agentApi.access: token it needs a token like the rest of the agent API.

Exposing a local MCP server

class {
	function configure() {
		return {
			exposes : "mcp",
			path    : "/mcp/tools",
			target  : "local-server"   // must match an mcp/*.bx entry's file name
		};
	}
}

You'll write that mcp/*.bx entry in Hosting and Consuming MCP Servers.

Exposing the web chat UI

// gateways/chat.bx
class {
	function configure() {
		return {
			exposes : "webui",
			path    : "/chat"
		};
	}
}

This one is big enough to earn its own lesson - see The Web App.

The agent API that is always there

Every built agent also serves one HTTP API at /__bxagents, with no gateways/ entry at all. The web page, the terminal chat (chat --server and --connect) and the chat platforms use it too, so anything they do, your program can do: run a turn (invoke, stream, events), stop or steer a run, answer an approval, list and fork conversations, keep notes, run slash commands. It describes itself: GET /__bxagents/openapi returns an OpenAPI document of the routes your server really has. A BoxLang client, bxModules.bxagents.models.api.AgentApiClient, has one method per route. The tour, with curl and BoxLang examples, is in Call your agent from code.

Who may call it is set under agentApi in Agent.yml:

  • Open (the default): anyone who can reach the port, all as one user, local. Fine on your own machine. bxAgents serve listens on 127.0.0.1 by default and warns if an open API listens on an address other machines can reach.
  • Behind tokens: callers send an x-bxagents-token header. tokenEnvVar: MY_SECRET names an environment variable holding one shared secret (it acts as an admin). tokens: true also accepts tokens made per person or program with bxAgents token create --name=laptop (--scope=chat or admin, --expires=90d). A token is shown once and only a hash is kept, so token revoke works at once on a running server.
  • Guards: allowedIPs lets only listed addresses and networks in at all, and rateLimit: { requests, perSeconds } caps calls per address. Both work open or closed.
  • Managing tokens over the API: manageTokens: true (with tokens: true) lets an admin make and revoke tokens remotely, and shows the Tokens tab of the web app's Access page.
  • Moving it: path: /agent-api serves the whole API somewhere else.
agentApi:
  tokenEnvVar: AGENT_ADMIN_TOKEN   # the NAME of an environment variable
  tokens: true
  allowedIPs: [ "10.0.0.0/8" ]
  rateLimit: { requests: 60, perSeconds: 60 }
bxAgents token create --name=laptop --expires=90d
bxAgents chat --connect=https://agent.example.com --token=<the token>

Full list of settings: Agent.bx.

Validation

exposes must be agent, mcp, or webui; path is required and must be unique across every exposure entry; an mcp exposure's target must match a real mcp/* entry's file name.

Try it

Acme support already has the web app's gateway from The Web App. Add a plain HTTP entry point of your own at a path you choose (add gateway api writes the exposes: "agent" file above), rebuild and serve it (Terminal 3):

cd ~/bxagents-course/acme-support
bxAgents add gateway api --path=/api/chat
bxAgents build
+ Created  gateways/api.bx
...
Build succeeded: <anything>/acme-support/.build/app
cd ~/bxagents-course/acme-support
bxAgents serve --port=8791

Call your own route, then the one that is always there. Both answer the same way:

curl -s -H 'Content-Type: application/json' -d '{"input":"hello"}' http://127.0.0.1:8791/api/chat/invoke
echo
curl -s -H 'Content-Type: application/json' -d '{"input":"hello"}' http://127.0.0.1:8791/__bxagents/invoke
echo
curl -s http://127.0.0.1:8791/__bxagents/openapi | grep -o '"title":"acme-support[^"]*"'
{"output":"Hello from the practice model. I am scripted: I only say what the course told me to say.","threadId":"<anything>","success":true}
{"output":"Hello from the practice model. I am scripted: I only say what the course told me to say.","threadId":"<anything>","success":true}
"title":"acme-support agent API"

The OpenAPI document lists every route the server really has, so any OpenAPI tool can call the agent. From BoxLang, the client has one method per route. Save this next to the project and run it:

import bxModules.bxagents.models.api.AgentApiClient;

client = new AgentApiClient( url = "http://127.0.0.1:8791" );
println( client.invoke( "hello" ).output );
println( client.about().name );
cd ~/bxagents-course
boxlang ask.bxs
Hello from the practice model. I am scripted: I only say what the course told me to say.
acme-support

The terminal chat can talk to a running server too (bxAgents chat --connect=http://127.0.0.1:8791), and has the same conversations as the web app. Who is allowed to call all this is the subject of Who Is Allowed. The whole tour, with every route, is Call your agent from code.

Checkpoint

acme-support/gateways/ has web.bx and api.bx. kit/resume.sh 13-exposing-http-and-mcp restores them; rebuild after.

Full reference: gateways/.

Next: Connecting Chat Platforms

Edit this page Download Markdown Last updated Nov 26, 2026, 8:45:51 AM Report an issue
Lesson 19 of 29 - Introduction to BxAgents