Move an in-process tool to a tool server
Take a tool function and its API key out of your agent and put them on a tool server behind the Amiril gateway. The agent keeps one gateway token.
An in-process tool is a function in your agent's own code. It calls an API with a key from the agent's environment, so anything that runs there can read the key. This guide moves the function onto a tool server behind the Amiril gateway, and the key goes with it.
The example is a payments agent with one function, issuePayment. It becomes the issue_payment tool.
Before you start
- A registered agent in Amiril. See register an agent.
- The admin role. See members and roles.
- Node 24 or later, to run the agent and the server as TypeScript.
Start from the tool in the agent
// A tool that runs inside the agent's own process and has the payments
// API's key. The starting point on /tool-servers/move-a-tool.
const { API_URL, API_KEY } = process.env;
/** Pays an amount to a payee. The agent's tool loop runs it when the model asks. */
export async function issuePayment(payee: string, amount: number): Promise<string> {
const response = await fetch(`${API_URL}/payments`, {
method: "POST",
headers: { authorization: `Bearer ${API_KEY}`, "content-type": "application/json" },
body: JSON.stringify({ payee, amount }),
});
return response.text();
}The key in API_KEY can do anything the payments API allows, but the agent only needs to issue a payment.
Put the function on a tool server
Follow wrap an API as a tool server through the step where you select Add 2 tools. Stop there, before it grants the tools. For now, give the server the same API_URL and API_KEY the agent uses. You replace that key in the last step.
The example server's issue_payment tool makes the same request as issuePayment and takes the same two arguments. For your own function, write a tool in server.ts that does the same. The example's other tool, get_payment, only reads payments. This agent never read payments, so the next step grants it issue_payment alone.
Grant the tool
Open the payments agent's Access tab and set Show to All tools. Turn on the switch for payments.issue_payment. Grant tools to an agent covers the rest of that tab.
Point the agent at the gateway
On the agent's Connect tab, select Create token, then Copy token.
Copy the Gateway URL from the same tab. Where the agent runs, set the URL as AMIRIL_GATEWAY_URL and the token as AMIRIL_GATEWAY_TOKEN.
Replace the function with a connection to the gateway. If your agent framework has its own MCP client, give it the Client configuration from the Connect tab and skip this code. Otherwise install the MCP SDK in the agent's project:
npm install @modelcontextprotocol/sdkThen use its client. listTools returns the tool definitions to pass to the model, and callTool runs a call the model asks for:
// The agent after its tool moved to a tool server: it has a gateway token
// and no API key. Shown on /tool-servers/move-a-tool.
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
const { AMIRIL_GATEWAY_URL, AMIRIL_GATEWAY_TOKEN } = process.env;
if (!AMIRIL_GATEWAY_URL || !AMIRIL_GATEWAY_TOKEN) throw new Error("Set AMIRIL_GATEWAY_URL and AMIRIL_GATEWAY_TOKEN.");
const gateway = new Client({ name: "payments-agent", version: "1.0.0" });
await gateway.connect(
new StreamableHTTPClientTransport(new URL(AMIRIL_GATEWAY_URL), {
requestInit: { headers: { authorization: `Bearer ${AMIRIL_GATEWAY_TOKEN}` } },
}),
);
// The tools you granted this agent. Hand them to the model in place of the old function.
const { tools } = await gateway.listTools();
console.log(tools.map((tool) => tool.name));
// When the model asks for the tool, the gateway checks the grant and calls your server.
const result = await gateway.callTool({ name: "payments__issue_payment", arguments: { payee: "Acme Ltd", amount: 500 } });
console.log(result.content);
await gateway.close();Run the agent with the two variables set:
AMIRIL_GATEWAY_URL=https://mcp.amiril.ai/mcp AMIRIL_GATEWAY_TOKEN=amiril_live_your_token node agent.tsIt prints the one tool it was granted and the payment API's answer:
[ 'payments__issue_payment' ]
[
{
type: 'text',
text: '{"id":"pay_1","payee":"Acme Ltd","amount":500,"status":"sent"}'
}
]The model now knows the tool as payments__issue_payment, the name the gateway gives it. Grant tools to an agent explains the name. Update any prompt that names the tool.
Take the key out of the agent
Delete issuePayment from the agent and remove API_URL and API_KEY from its environment. Then make a new key at the payments API, restart the server with it and delete the old one. The old key lived in the agent's environment, and a copy may still sit in a log or a container image.
Next steps
Wrap an API as a tool server
Put a small MCP server in front of an API that has none, so you can grant its actions to your agents as tools. About sixty lines of TypeScript.
Connect an MCP server with an API key
Register a tool server that takes a fixed API key or token in a header. You enter the key once in Amiril, and the gateway adds it to every call.
Grant tools to an agent
Turn on the tools an agent may call, give it a gateway token and point its MCP client at the Amiril gateway. Then revoke the tools it stops using.
Add a tool server
Register the MCP server where your agents' tools live and choose how Amiril signs in to it. Then grant its tools to agents on their Access tab.
Last updated on