DEV Community

Hunter
Hunter

Posted on Edited on

Don't expose your REST APIs 1-to-1 in your MCP server

Typically a team will map each REST API endpoint to its own MCP tool. All of a sudden a model has access to tools for creating, reading, updating and managing roles for users, and listing and adding workspaces.

Whilst this does allow a server to be set up quickly, it soon unravels during testing.

Front-end applications know exactly how to interact with a REST API. Developers have manually created TypeScript which dictates an application’s behaviour, such as the order in which requests are made, how to represent states (loading, success, error) and how to respond to errors. LLMs however do not compile code. As they must dynamically assemble low-level tools, they must infer request ordering, manage state and independently manage errors.

Three problems with 1-1 mapping of CRUD tools

Token usage

All MCP tool schemas are included within the system prompt. Single REST API endpoints – and indeed all of their child objects, query fields and enums – use between 150 and 300 tokens. If there are 40 or more endpoints then prior to even beginning a user’s input, the prompt has already used 8,000-12,000 tokens. Alongside this, having many similar schemas in context reduces the precision with which a model can detect the most appropriate tool. Being presented with tools such as users_update, users_patch and memberships_update causes a model to waste effort analysing the slight variations between these schemas. This in turn distracts it from the core issue of meeting user needs.

Process deviation

In practice, processes almost always consist of a number of stages. For example, onboarding a team member involves locating the correct workspace ID, creating a user, identifying the relevant role ID and associating the user with the role. Finally an invitation email must be dispatched. With low-level tools, a model has to perform and coordinate these stages using four or five distinct API requests. Each request-response exchange increases response times and provides further opportunity for a model to become disengaged from previous results. Should stage three (email dispatch) fail due to, for instance, an email domain being blacklisted and an API therefore returning a 422 error, a model is seldom aware of how to reverse stage two. It will typically become stuck in a loop re-attempting that stage, or else abandon the process. Consequently, your database may end up containing a user entry that does not include workspace authorisation information.

Parameter inference

Whilst standard REST APIs provide access to detailed information, front-end applications usually shield users from this. APIs commonly expect users to input specific values for parameters, such as UUIDs, page ‘pointers’, and boolean values including include_archived and dry_run which are generally reserved for administrative interfaces. In the absence of comprehension regarding certain parameters, models infer suitable values. For example, they may incorrectly apply 0-indexing to an API expecting 1-indexing or fabricate a realistic seeming UUID. Unless tool parameters are restricted to align with business aims, models will therefore tend to invent information.

Business orientated tools

The solution is to avoid exposing database entities and instead expose business processes.

Merge pertinent low-level tools into a higher level tool that reflects the true user goal. Illustrate this by contrasting the on-boarding of a team member using low-level tools and a business process tool:

// Bad: Exposing individual REST endpoints as MCP tools
[
  { "name": "users_create", "inputSchema": { "properties": { "email": {}, "org_id": {} } } },
  { "name": "roles_list", "inputSchema": { "properties": { "org_id": {}, "page": {} } } },
  { "name": "memberships_attach", "inputSchema": { "properties": { "user_id": {}, "role_id": {} } } }
]
Enter fullscreen mode Exit fullscreen mode
// Good: One transactional workflow tool shaped around the business goal
{
  "name": "invite_workspace_member",
  "description": "Creates a user if needed, assigns their role, and sends an invite in one atomic transaction.",
  "inputSchema": {
    "type": "object",
    "required": ["workspace_slug", "email", "role"],
    "properties": {
      "workspace_slug": { "type": "string" },
      "email": { "type": "string", "format": "email" },
      "role": { "type": "string", "enum": ["viewer", "editor", "admin"] }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Observe how much less inference is now required. The model merely needs to supply a workspace slug, an email, and a controlled role parameter – all of which it already possesses from the interaction.

Within the invite_workspace_member tool, your server manages the underlying complexity. Firstly it translates a workspace slug into an internal ID. Secondly it ascertains whether a user already exists. Thirdly it converts the role name "editor" to the relevant role row. Finally it simultaneously inserts both the user and role linkage into the database. Should any stage of this process encounter an issue, Postgres will automatically reverse the database transaction and return an informative error. Upon successful completion, the tool returns a succinct result object to confirm the invitation has been sent. There is just one request-response exchange and no incomplete data entries.

A basic principle for tool exposure

Consider the implications for a process should an intermediate stage fail, prior to introducing a novel tool to your MCP server.

Should a business process entail three consecutive API requests and contain a contingency for reversing the second of these should it fail, then encapsulate these within a unified server-side business process. Your server already includes features for database transactions, strongly typed SDKs and deterministic error processing. Utilise these. Allow your server-side code to execute business processes so that models can concentrate on deciphering user requirements.

Top comments (5)

Collapse
 
hunter-debug00 profile image
Hunter •

how do you handle it?

Collapse
 
techdevfun profile image
TECHDEV •

Small wrinkle in the good example: the Postgres transaction covers the user and role rows, but the invite email sits outside it either way. If the email goes out before the commit and the commit fails, an invite was sent for a user that no longer exists. If it goes out after the commit and the provider returns the 422 from your own process-deviation section, the user and role are committed with no invite sent. Either way the 'one atomic transaction' in the tool description stops at the database edge.

How do you handle that step in practice? The description already says 'creates a user if needed', which reads like a re-run would find the existing user and just retry the invite. Is re-running the same tool with the same inputs the intended recovery, or does the tool return a partial-success result so the model knows the invite is the only step left?

Collapse
 
agentwareshq profile image
agentwares •

Good framing — one gap I'd add is on the slug lookup. invite_workspace_member removes the UUID guessing, but the model still has to produce the right workspace_slug from how the user talks about it ('the marketing workspace'). If two workspaces have similar slugs, the model will guess instead of asking. I'd have the tool resolve display names server-side and return a structured needs_disambiguation result — the candidate slugs and nothing else — rather than picking one silently. That keeps the single round trip while making the ambiguity explicit.

Related: once the workflow is one tool, the per-step trace disappears from the MCP conversation. Worth logging each stage server-side with a correlation id returned in the result, so when the invite step fails you can still tell from the trace whether it was the role lookup, the insert, or the email provider — otherwise debugging the composite tool is harder than debugging the CRUD tools it replaced.

Collapse
 
paul_irolla_6b5ae261d0224 profile image
Paul Irolla •

The token side of this is easy to miss. Every tool schema goes into each request, so each endpoint you expose is paid for on every call, including the calls that never use it. Collapsing them into a few business tools shrinks that fixed cost before anything else changes.

Collapse
 
osamamalik profile image
Osama Malik •

One caveat when you collapse endpoints into business tools: keep the tool annotations honest. The MCP spec has readOnlyHint and destructiveHint, and a combined tool like onboard_user should be marked destructive so clients can ask for confirmation. Also make the composite tool idempotent or give it an explicit rollback path, since a retry after a half finished workflow is the exact failure you describe. Returning a structured result that names which step failed gives the model something to act on instead of looping.