Back to the blog ·

Using a route optimization API from an AI agent with MCP

How an AI agent can plan routes through a Model Context Protocol server: the tool contract, what the server sends to the REST API, how to keep results short enough for an agent, and how to guard the bill. The MCP server is planned and launches with the API.

An AI agent that can only read text cannot plan a delivery day. It needs a tool: a named function with a typed input, which it can call and whose result it can read. The Model Context Protocol (MCP) is a common way to offer those tools to an agent. This post describes how a route optimization tool should work over MCP, using the Argamin REST API underneath.

Be clear about status first. The Argamin MCP server is planned and is not live yet. It launches with the API, and the tool names and behavior below are the design. Everything it calls is the REST API, which is described in the API reference and follows the same rules as the rest of this blog.

What the agent gets

The MCP server uses the same API key and the same billing as the REST API. An agent does not need its own account or a separate price. It needs one secret, set in the server’s configuration, and the server handles the HTTP.

The planned tools are:

Tool Does REST call
optimize_routes Fleet and single-vehicle optimization POST /optimize (or POST /optimize/jobs for large problems)
compute_matrix Travel times and distances POST /matrix
get_route Driving route between waypoints POST /route
snap_to_roads GPS map-matching POST /match
nearest_road Nearest road position POST /nearest
check_coverage Whether a point is inside a loaded region GET /coverage
get_job Status and result of a background job GET /optimize/jobs/{id}

The tool contract

The tool’s input schema mirrors the REST request, but with fewer fields and clear descriptions. An agent reads the description and fills the fields from the user’s request. Here is the shape of optimize_routes as an MCP tool definition:

{
"name": "optimize_routes",
"description": "Plan delivery routes for one or more vehicles. Returns a summary, one route per vehicle, and the list of stops that could not be served. Each stop is billed. Use one vehicle for a single route.",
"inputSchema": {
"type": "object",
"required": ["vehicles"],
"properties": {
"vehicles": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["id", "start"],
"properties": {
"id": { "type": "string", "maxLength": 128 },
"start": { "$ref": "#/$defs/Location" },
"capacity": { "type": "array", "items": { "type": "integer", "minimum": 0 } },
"skills": { "type": "array", "items": { "type": "string" } },
"time_window": { "$ref": "#/$defs/TimeWindow" }
}
}
},
"jobs": {
"type": "array",
"items": {
"type": "object",
"required": ["id", "location"],
"properties": {
"id": { "type": "string", "maxLength": 128 },
"location": { "$ref": "#/$defs/Location" },
"service": { "type": "integer", "minimum": 0 },
"delivery": { "type": "array", "items": { "type": "integer", "minimum": 0 } },
"time_windows": { "type": "array", "items": { "$ref": "#/$defs/TimeWindow" } }
}
}
}
}
}
}

When the agent calls the tool, the server sends the same body to POST /v1/optimize, with the same field names. A tool that renames fields would make the agent’s documentation and the API docs disagree, so the MCP server does not rename.

Two rules make the tool safer for an agent:

  • The description says that each stop is billed and that one vehicle is a single-vehicle problem. An agent that reads this will not add a dummy vehicle to get a different answer.
  • The required fields are the ones the REST API requires, so an incomplete request fails the same way it would over HTTP. Client errors are never billed.

Keep results short for the agent

An agent’s context window is limited. A route with 1,000 stops has a long steps array, and pasting it into the conversation fills the context with coordinates the agent does not need. The planned server therefore returns a short answer: the summary, the number of routes, the unassigned list with its reasons, and a link or id for the full result.

An agent then calls get_job for the full result of a large run, or asks for one route’s steps by vehicle id. The full data stays on the server, and the agent reads only what the user asked about.

This follows from how the REST API works. Async jobs run in the background, and a job’s result is kept for 7 days. The input is deleted when the job finishes, and at the latest after 24 hours.

Check coverage first

An agent that plans a route for a point outside the covered regions gets OUT_OF_COVERAGE back, which is not billed. It is cheaper and clearer to call check_coverage first, so the agent can say “this address is outside our loaded regions” before it tries to plan. The coverage list is at the coverage page, and the same data is available from GET /v1/coverage.

Guard the bill

An agent can make many calls in a short time, so set the limits before you let it run:

  1. Use a spend cap. The default monthly cap is $200. Lower it for an agent account, and raise it only when the usage justifies it.
  2. Send an Idempotency-Key. An agent that retries after a timeout should reuse the same key. A replay with the same body returns the stored answer and is not billed.
  3. Do not re-plan unchanged problems. Ask the agent to reuse the last result if the inputs have not changed. Each new run is billed.
  4. Use test keys in development. An am_test_ key is free and limited to 10 stops and 100 calls a day. It is enough to check that the agent calls the tool correctly.

Rate limits also apply: 60 requests per minute, and at most 5 concurrent jobs per account. The agent should honor Retry-After when it gets RATE_LIMITED or ENGINE_UNAVAILABLE.

A good first prompt for the agent

An agent that uses this tool should know three things: the coordinates it can use, the time zone of the shift, and what to do with the unassigned list. A useful system instruction looks like this:

Plan routes with optimize_routes. Use one vehicle for a single route. Send times with an explicit offset. If unassigned is not empty, report each stop and its reason, and do not re-run the plan until the user changes an input.

That instruction keeps the agent honest about failures, and it stops a common loop where an agent re-runs a plan hoping for a different answer.

Where this fits

If your agent is a dispatch assistant, a planning copilot or a no-code automation with an AI step, the MCP tool is the simplest way to add route planning without writing HTTP code. The same request shape works from n8n, Zapier or Make, and from a plain script. For the request itself, read time windows and capacities. For cost, read the pricing explainer. For the difference between one vehicle and many, read VRP vs TSP.

The MCP server launches with the API. To be told when it is live, join the waitlist.

Compare the cost on the pricing page, or join the waitlist.