AI agents · OpenClaw · self-hosting · automation

Quick Answer

How Do AI Agents Connect With APIs and External Tools?

Published:

The short answer

An AI agent connects to an API by asking for a tool call; your code, not the model, makes the request. The loop is always the same:

  1. Describe the tool — name, what it does, a JSON schema for its inputs.
  2. The model chooses — given the user’s goal, it returns call get_invoice(customer_id="C-102") instead of prose.
  3. The runtime executes — it validates the arguments, attaches credentials, calls the real API.
  4. The result goes back — the response (trimmed) is appended to the context; the model decides the next step or answers.

What changes between setups is how tools are packaged and where credentials and policy live. As of October 18, 2026 there are four common ways to do it:

ApproachHow it worksUse it when
Native function callingTool schemas passed in the API request; your code dispatches callsOne app, one model provider, a few internal APIs
MCP serverTools served over the Model Context Protocol; any MCP client discovers and calls themSame tools used from several clients (Claude, ChatGPT, Cursor, your agent)
Integration platformComposio, Arcade or Pipedream Connect provide thousands of prebuilt tools with per-user OAuthAgents acting in many SaaS apps on behalf of many users
Tool gatewayAWS AgentCore Gateway, Cloudflare AI Gateway, an API gateway in front of MCP servers; holds credentials, enforces policy, logsProduction and enterprise deployments

Step by step: connecting an agent to an API safely

1. Wrap the API as a narrow tool

Expose create_refund(order_id, amount) rather than a generic http_request(url, body). Narrow tools are easier for the model to use correctly and far easier to secure. Return compact JSON, not whole web pages — every byte you return is tokens you pay for.

2. Serve it over MCP

The 2026-07-28 MCP specification is the current stable version and the largest revision since launch: a stateless core that scales on ordinary HTTP infrastructure, plus extensions for server-rendered UIs (MCP Apps) and long-running work. The official SDKs (TypeScript, Python and others; Ruby reached 1.0) let you define tools in a few lines. Once served over MCP, the same tool works in Claude, ChatGPT, coding agents and frameworks such as LangGraph or the OpenAI Agents SDK. Useful ready-made servers are listed in best MCP servers.

3. Handle authentication outside the model

  • Service credentials (your own database, internal APIs): keep them in a secrets manager and inject them at call time. See best secrets managers for AI agents.
  • User credentials (their Gmail, Slack, Salesforce): use delegated OAuth with a token store. This is what Composio, Arcade and Pipedream Connect sell. On AWS, AgentCore Identity does this and is free when used through AgentCore Runtime or Gateway.
  • Never paste a key into the system prompt: anything in context can be leaked by prompt injection.

4. Put a gateway and policy in front

A gateway gives you one place to log every call, rate-limit, and enforce rules such as “refunds over $25 need a human”. AgentCore Policy checks each tool call through AgentCore Gateway against your rules; ServiceNow’s AI Passport and similar control planes do the same across vendors (see ServiceNow AI Control Tower alternatives).

5. Require approval for irreversible actions

Payments, deletes, emails to customers: pause and ask. Frameworks with checkpoints (LangGraph, durable runtimes) can wait hours for approval and resume.

6. Test the tool, not just the model

Write evals that check the agent picks the right tool, passes valid arguments and recovers from API errors (timeouts, 429s, empty results). See how to monitor and evaluate AI agents in production.

Common mistakes

  • Too many tools. Dozens of overlapping tools confuse the model; load tools per task or use tool search.
  • Huge tool outputs. Return the five fields the model needs.
  • No idempotency. Retries can double-charge; pass idempotency keys on write calls.
  • One shared identity for every agent. Give each agent its own scoped credentials so you can revoke one without stopping all.

For where these agents run, see best platforms for building and deploying AI agents; for protocol background, MCP vs A2A vs ACP.

Last verified: October 18, 2026.

Sources