How to Wrap Your Own API in an MCP Server
What of your API is worth exposing to an agent, why tool descriptions decide more than the code, how authentication works in such a wrapper, and which limits are mandatory.
All articles in the guide MCP-серверы · 11
Wrapping your API in MCP is less a technical task than a design one. The wrapper code is quick; the work is deciding what you expose and how.
What to expose
The temptation is to mirror the API one to one. Do not.
Expose tasks, not endpoints. You may have three calls to fetch an order, its lines and its delivery status. The agent needs one tool: “get order with contents”. Three tools instead of one means three steps, three context hits and three chances to get it wrong.
Expose what is actually needed. If the agent handles four kinds of task, there should be roughly four tools, not thirty. The rest is context cost and extra ways to choose wrongly.
Do not expose dangerous operations at the start. Deletion, bulk updates, anything involving money. Those get added deliberately, one at a time, usually with human confirmation - see security.
A useful test before adding a tool: can you name the specific agent question it answers? If not, it is not needed.
Descriptions decide everything
This is the central point. The agent chooses a tool by its description, and that matters more than the code behind it.
A good description answers four questions:
- What it does - one sentence.
- When to use it - concretely, with a trigger: “when the order number is known”.
- When not to use it - the most skipped item and the most valuable. It prevents redundant calls.
- What comes back, including the “nothing found” case.
Bad: “Order handling.”
Good: “Returns an order and its line items by number. Use when the number is known. There is a separate tool for finding a customer’s orders. If the order is not found, returns an empty result rather than an error.”
The same applies to arguments: every parameter needs a description clear enough that the agent knows what to put there. A type parameter with no enumerated values guarantees mistakes.
And once more: one tool does one thing. A tool with a “mode” parameter taking five values is five tools the agent will confuse.
Authentication
Two scenarios, structurally different.
The server acts as one technical user. The token sits in an environment variable and is used for every call. Simple, and fine for internal work. That user’s permissions should be minimal.
There are several users. The token is supplied at session initialisation and the server acts on behalf of a specific person. More complex, but necessary wherever access separation matters.
A rule common to both: the secret must not enter the agent context. Do not build a tool that takes a token as a parameter: the agent will see it, and it will travel to the model provider along with everything else.
Limits
Mandatory, not optional. A tool response enters context whole, and without limits one call can consume the window.
What to limit:
- Records per response, with an explicit note that the result was truncated and a way to fetch the next page.
- Text field length. The agent does not need a three-screen product description.
- Call count. Protection both against a looping agent and against load on your system.
- Heavy operations. A yearly report is better returned as a summary than row by row.
A related recommendation: compute on your side. An agent handed a thousand rows to sum them will spend context and get it wrong. A tool that returns the computed figure is cheaper and more accurate.
Errors
Return text explaining what went wrong and what to do. “No customer found for that phone number” is more useful than a 404: the agent reads it and refines the query. An empty response with no explanation will most likely be read as “no data” and completed with invention.
How to write the server itself: an MCP server in Python. A worked example on a specific system: an MCP server for 1C. The overview is in the MCP guide.
FAQ
Should I expose the whole API to the agent?
No, and that is the classic mistake. Thirty endpoints become thirty tools that occupy context and confuse the agent. Expose operations phrased in terms of the tasks the agent actually performs, not a mirror of your API.
How is authentication passed to an MCP server?
Through environment variables when the server acts as a single technical user, and through a session token when there are several users. The key requirement: the secret must never enter the agent context, because from there it goes to the model provider.
Why limit response size?
Because a tool response enters context in full. One call returning a thousand records eats the window and makes the next step worse and more expensive. Record limits and truncation of long fields belong on the server, not in the hope that the agent will be careful.
- MCP Servers: What They Are and Why They ExistGuide
- An MCP Server for 1C: Connecting an Agent to an Accounting SystemWhat users of the 1C accounting platform want from an AI agent, the options for reaching the data, how to wrap HTTP services and OData, and where the permission and security lines sit.
- How to Connect an MCP Server to Claude Code and CursorWhere MCP configuration lives, how to connect a server step by step, how to confirm the agent really sees it, and what to do when the server never appears.
- Running a Local MCP ServerHow a local MCP server differs from a remote one, how it is launched, how to limit its file access, and how to debug it when the client shows no errors.
Done for you
I will connect your services and data to AI through MCP
A custom MCP server for your CRM, database or internal API, with access rules and logs.
from $1,500 · 1 to 2 weeks