Sub topic 13
Choosing servers and keeping context lean
This chapter assumes you can edit an mcpServers configuration, know the difference between tools, resources and prompts, and have read the security chapter on trust and permissions. It covers two linked decisions: which servers to add, and how to stop them from using up the model's context window.
Why every server has a cost
A server costs more than the time to install it. Each connected server adds text to the model's context window, which is the fixed amount of text the model can consider at once. Three things use that space:
- Tool definitions. After the handshake, the client calls
tools/list. The name, description and input schema of every tool are usually placed in the model's context on every request, so the model knows what it can call. A server with 30 tools can add thousands of tokens before you type anything. - Tool results. Whatever a tool returns is added to the conversation. A tool that returns a 2,000-line file or a full database table can fill much of the window in one call.
- Resources and prompts you pull in. These cost space only when the user or the assistant attaches them. They are cheaper by default than tools, because nothing is sent until someone asks.
The costs are paid in three ways:
- Money and speed. More input tokens mean slower and more expensive requests.
- Accuracy. Models pick the wrong tool more often when many tools have similar names or descriptions.
- Room for your work. Space used by tool definitions is not available for your code, the conversation history or the model's reasoning.
The numbers below are illustrative. Measure your own setup, because token counts depend on the assistant and the model.
| Setup | Servers | Tools each | Approx. tokens per tool | Fixed overhead per request |
|---|---|---|---|---|
| Minimal | 1 | 5 | 150 | about 750 |
| Typical | 4 | 12 | 200 | about 9,600 |
| Overloaded | 10 | 25 | 250 | about 62,500 |
Decide whether you need a server at all
Before adding a server, check the alternatives. A server is justified when it gives the assistant a capability it cannot get more cheaply.
- The assistant can already do it. Many coding assistants read files, search code and run shell commands. A filesystem server on top of that adds little.
- A command-line tool covers it. If the assistant can run
git,gh,psqloraws, a short instruction like "use theghCLI for issues" may replace a whole server. The CLI's help text is loaded only when it is used, and you pay nothing while it is idle. - A server gives structure the shell cannot. Typed inputs, authentication handled outside the model, paginated results, or access to an app that has no CLI are good reasons to use a server.
The shell is not always the cheaper or safer option. A server can restrict the assistant to a narrow set of actions. A raw shell can do anything the user can do. If the capability involves sensitive data, a narrow server may be the safer choice even if it costs more context.
A checklist for evaluating a server
Apply these questions before you add a server to your configuration.
Trust and maintenance
- Who publishes it, and can you read its source? A server launched with
npxoruvxruns code from a package registry with your user's permissions. - Is it maintained? Check the date of the last release and whether issues get answers.
- Does it need secrets? If so, can you give it a token with the minimum scope, set through the
envfield and not written in the file?
Capability fit
- Does it expose read-only tools or write tools? Prefer a server, or a mode of a server, that can only read when read access is enough.
- Can you limit its scope? The reference filesystem server takes directory arguments, so you can restrict it to one project.
- Does it use the right primitive? Static reference data that the user picks fits a resource. A fixed workflow the user triggers fits a prompt. Actions the model decides to take are tools.
Context footprint
- How many tools does it expose, and how long are the descriptions?
- Can you switch off the tools you do not use? Many clients let you allow-list tools per server. Check your assistant's documentation, because this feature is client-specific and not part of the protocol.
- Are the results bounded? Look for parameters such as
limit,offset, or a maximum size.
Scope servers to the work
Most assistants let you set configuration at more than one level, such as per user and per project. The exact file names and precedence rules depend on the assistant. A useful pattern is:
- Keep the user-level configuration small, with only servers you use in almost every session.
- Put project-specific servers, such as a database server pointing at one dev database, in the project configuration.
- Disable servers you are not using for the current task. Some clients have a toggle for this. If yours does not, comment out the entry by keeping a second configuration file and swapping them.
This also improves security. A database server with write access should be present only in the project that needs it, not in every session you open.
The following project configuration limits the filesystem server to one directory, and loads one other server only for this project:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/project"]
},
"my-api": {
"command": "python",
"args": ["server.py"],
"env": { "API_TOKEN": "<set-in-your-environment>" }
}
}
}Design your own tools to be lean
If you write servers, you control a large part of the context cost. Both the definition and the result matter.
Keep definitions short
The description is read by the model on every request. State what the tool does and when to use it, in one or two sentences. Do not paste usage manuals into it. Prefer a small number of well-named tools over many near-duplicates. For example, one search_logs tool with a level parameter is better than search_error_logs, search_warning_logs and search_info_logs.
Bound the results
Never return unbounded data. Accept a limit, apply a default, and tell the model when output was cut so it can ask for more. The example below does this with the Python SDK:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("log-search")
LOG_LINES = [f"line {i}: example log entry" for i in range(1, 5001)]
MAX_LIMIT = 100
@mcp.tool()
def search_logs(query: str, limit: int = 20) -> str:
"""Search log lines containing the query. Returns at most `limit` lines (max 100)."""
limit = max(1, min(limit, MAX_LIMIT))
matches = [line for line in LOG_LINES if query in line]
shown = matches[:limit]
result = "\n".join(shown)
if len(matches) > len(shown):
result += f"\n[showing {len(shown)} of {len(matches)} matches; narrow the query or raise limit]"
return result
if __name__ == "__main__":
mcp.run()The trailing note matters. Without it, the model may treat a truncated result as the full answer.
Return summaries, then details
For large objects, return a short list of identifiers and titles first, and offer a second tool or a resource to fetch one item in full. The model then pays for detail only on the item it needs. This is the same pattern as search results followed by opening a page.
Use pagination and list-change notifications carefully
The protocol supports cursor-based pagination for list operations, and servers can send a notifications/tools/list_changed message when the set of tools changes. Dynamic tool lists can keep the context small, because a server can expose only the tools relevant to the current state. They also change the context mid-session, so use them only when the benefit is clear, and make sure your client handles the notification.
Measure, then trim
Do not guess at overhead. Use these steps to find it:
- Start a fresh session with no servers and check the token usage your assistant reports, if it has such a display.
- Add one server and check again. The difference is that server's fixed cost.
- Run the Inspector against the server (
npx @modelcontextprotocol/inspector python server.py) and read thetools/listresponse. Long descriptions and large schemas are easy to spot there. - Call the tools you use most and look at the size of the results.
Then trim in this order: remove servers you do not use, disable tools you do not use, scope the rest to the project, and last, fix or replace servers with oversized tool results.
Decision flow
The flow below summarises the choice for a new capability.
Common mistakes
- Installing every server you find. Each one adds fixed overhead and more chances for the model to choose the wrong tool.
- Leaving write-capable servers enabled globally. Enable them per project, and prefer read-only credentials.
- Judging a server by its feature list. A server with 60 tools may cost more than it is worth if you use three of them.
- Ignoring result size. A single unbounded result can cost more than all tool definitions put together.
- Assuming tool allow-lists are standard. They are a client feature. Check how your assistant works before relying on one.
Exercise
Your assistant has four MCP servers connected. Sessions feel slow, the model often picks the wrong tool, and one server (a database tool with 28 tools, of which you use 2) is the largest source of overhead. List three steps, in order, that reduce context cost without losing the capabilities you use. Then name one change you would make to a server you write yourself to cut result size.
Show answer
First, remove or disable any server you have not used recently. Second, disable the 26 unused tools on the database server if the client supports an allow-list, or replace it with a narrower server that exposes only what you need. Third, move the database server from the user-level configuration to the project configuration so it loads only where it is needed. For your own server, add a limit parameter with a default and a maximum, and append a note when results are truncated so the model knows more data exists.