Gateways and composition
One gateway in front of many servers: tool names that collide, a prefix that keeps them apart, an allow-list that cuts the tool list's token cost, and a call routed to the right server or the wrong one.
Check yourself ↓ three questions and what to remember, at the end
Loading the animation…
Concept
A host with ten MCP servers holds ten connections, ten sets of credentials and ten tool lists. A gateway collapses them into one: it is an MCP server to the host and an MCP client to each server behind it. It lists their tools, merges them, and routes each tools/call to the server that owns the tool. Step through the animation: one tools/list from the host fans out to the 10 tools of four servers, and comes back as one list.
Names collide. Tool names are only unique within a server. Here files, github and web all offer search, and files and github both offer read_file. The spec's tools page (accessed 2026-10-08) says it plainly: aggregators "MAY encounter naming collisions" and "SHOULD implement a disambiguation strategy such as prefixing tool names with a server identifier", and the server's own serverInfo.name "SHOULD NOT be relied upon" for it, since it need not be unique.
- flat: the first server registered wins each name. The host sees 7 tools; three are shadowed, silently. When the model asks for
searchmeaning the web, the gateway sends it to files, and the model gets "no matches in the project" back: a wrong answer, not an error. - prefix: every tool is exposed as
alias.toolwith an alias the gateway chooses (the MCP series writesgithub__search; any separator among the characters the spec allows for tool names, letters, digits,_,-and., will do). All 10 are reachable andweb.searchgoes to web. - filtered: prefixed, and only an allow-list for the task is exposed: 3 tools.
The tokens are the bill. The model reads every exposed definition in its prompt on every turn. The four servers' lists cost 651 tokens together (Qwen2.5 tokenizer); prefixing every name costs a little more (655), while flat shadowing "saves" tokens only by losing tools (472). Filtering to what the task needs cuts the list to 178. The same cost seen from the harness's side, and what it does to the context window, is Agent Harnesses Explained, the context budget.
What else a gateway does. It is also where a deployment puts authentication to the servers, audit logs and policy: the OAuth_for_MCP deck walks through a real one (Docker's MCP Gateway), and the two playgrounds below run an aggregating gateway and a per-server bridge in Docker Compose.
Caching the lists, and keeping them in order
A gateway answers the same tools/list for many hosts, so it is the natural place to cache it. The 2026-07-28 revision makes that explicit with a CacheableResult: tools/list, prompts/list, resources/list, resources/templates/list, resources/read and server/discover must now carry two hints (changelog, caching, accessed 2026-10-16):
ttlMs, how long the result may be considered fresh, "analogous to HTTPCache-Control: max-age". It is a freshness hint, not a promise, and not a polling interval;0means immediately stale. It complements thelistChangednotifications rather than replacing them.cacheScope,"public"or"private". A public result has no user-specific data, and "any client, shared gateway, or caching proxy" may serve it to any user. A private one may be reused only for the same authorisation context: caches "MUST NOT be shared across authorization contexts". A gateway that filters tools by the caller's scopes (chapter 6) is serving private lists, whatever its servers said.
The same revision adds a SHOULD that matters to the model as much as to the gateway: servers should return tools/list in a deterministic order, the same order whenever the set of tools has not changed, because that "enables clients to reliably cache the tool list and improves LLM prompt cache hit rates when tools are included in model context" (tools). That is the "tools reordered every turn" layout of Agent Harnesses Explained, prompt caching, where rotating the tools every turn throws away the cached prefix after the system text, prevented at the source. A gateway keeps the promise only if its own merge is deterministic too: this engine merges in server-registration order, so the same four servers always give the same list.
Maths
With servers each exposing tools of about tokens each, the list costs tokens on every model call, so over a session of calls the tool list alone costs input tokens (before any prompt caching). Prefixing changes only by the extra name tokens; filtering changes it by the fraction kept, here 3 of 10 tools.
Shadowing is a probability problem for the model as well as a routing one: if the model picks among same-named tools by name alone, a flat merge maps all of them to one server, so the call is right only when the model meant the first-registered server.
Code
The merge, as the vendored engine does it:
const exposed = policy === "flat" ? (t.name as string) : s.alias + "." + t.name;
let status = "exposed";
if (policy === "flat" && exposed in owner) status = "shadowed";
else if (policy === "filtered" && !ALLOW.includes(exposed)) status = "filtered";
The host's call under prefix, as it reaches the gateway:
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "web.search",
"arguments": { "query": "MCP gateway tool name collisions" },
"_meta": {
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
"io.modelcontextprotocol/clientInfo": { "name": "example-host", "version": "1.0.0" },
"io.modelcontextprotocol/clientCapabilities": {}
}
}
}
In production. A server written with the official MCP Python SDK (2.3.0) sets the cache hints per method; the tool's schema is generated from the function's signature and docstring:
from mcp.server import MCPServer
from mcp.server.caching import CacheHint
mcp = MCPServer(
"notes",
version="1.0.0",
# 2026-07-28: the tool list is the same for every user, so gateways may share it for a minute
cache_hints={"tools/list": CacheHint(ttl_ms=60_000, scope="public")},
)
@mcp.tool()
def search_notes(query: str, limit: int = 5) -> list[str]:
"""Search the team's notes; return the titles of the best matches."""
notes = ["Cache layout", "Retry budget", "Sandbox rules"]
return [n for n in notes if query.lower() in n.lower()][:limit]
if __name__ == "__main__":
mcp.run() # stdio
Run here over stdio with the SDK's own client in 2026-07-28 mode, its tools/list result carried "ttlMs": 60000, "cacheScope": "public", "resultType": "complete"; without cache_hints the SDK sends the safe defaults, 0 and "private".
Check yourself
Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.
Reveal the answer
Answer: 7 (counted right within 10%)
Three are shadowed, silently: search is offered by three servers and
read_file by two. Prefixing every name with an alias makes all
10 reachable.
Commit to a number, then reveal.
Reveal the answer
Answer: files
files, the first registered: the model gets "no matches in the project"
back, a wrong answer rather than an error. With prefixes, web.search goes
to web.
Pick one, then reveal.
Reveal the answer
Answer: 27% (counted right within ± 5%)
178 tokens against 651, and the model reads them on every turn. Flat shadowing also "saves" tokens, but only by losing tools.
Commit to a number, then reveal.