What you will build
This guide walks through the anatomy of a working AI agent — one that can take a natural-language goal, plan, call tools, and return a result. We will keep the code illustrative and focus on the structure that matters, because the structure is what transfers between frameworks and models.
The four moving parts are always the same: a model to reason, a loop to drive it, a set of tools to act with, and safety controls around those tools.
The agent loop
At its heart an agent is a loop around a model call. Each turn, you hand the model the conversation plus the list of available tools; it either answers or asks to call a tool; you run the tool and feed the result back; you repeat until it produces a final answer.
import Anthropic from '@anthropic-ai/sdk'
const client = new Anthropic()
async function runAgent(goal: string, tools: Tool[]) {
const messages = [{ role: 'user', content: goal }]
while (true) {
const res = await client.messages.create({
model: 'claude-sonnet-5',
max_tokens: 1024,
tools: tools.map((t) => t.schema),
messages,
})
const toolUse = res.content.find((c) => c.type === 'tool_use')
if (!toolUse) return res // final answer
const result = await runTool(toolUse.name, toolUse.input)
messages.push({ role: 'assistant', content: res.content })
messages.push({ role: 'user', content: [{ type: 'tool_result', tool_use_id: toolUse.id, content: result }] })
}
}That is the whole engine. Everything else — planning, memory, multi-agent coordination — is elaboration on this loop.
Adding tools
A tool is a name, a description, a typed input schema, and a function that runs the work. The description matters as much as the code: it is what the model reads to decide whether the tool fits.
const sendEmail: Tool = {
schema: {
name: 'send_email',
description: 'Send an email to a recipient',
input_schema: {
type: 'object',
properties: {
to: { type: 'string' },
subject: { type: 'string' },
body: { type: 'string' },
},
required: ['to', 'subject', 'body'],
},
},
run: async ({ to, subject, body }) => {
// ... integrate with your email provider
return { sent: true }
},
}Give each tool a clear, typed schema and the model will supply valid arguments without any prior knowledge of your systems.
Safety controls
Before a tool runs, validate it. A small gate in front of runTool handles permissions, risk and rate limits:
async function guard(action: { tool: string; input: unknown }) {
if (!hasPermission(action.tool)) throw new Error('not permitted')
if (needsApproval(action.tool)) await requestHumanApproval(action)
if (isRateLimited(action.tool)) throw new Error('rate limited')
}Pair this with an audit log of every call and a per-run cost cap, and you have the core of a production-safe agent. These are the same controls covered in depth in AI agent best practices.
Skip the plumbing with BusinessMCP
The loop above is the easy part. The hard part is the tools: securely connecting your CRM, analytics, ad platforms and Stripe revenue — and keeping tokens refreshed, permissions scoped, and calls logged — for every model you might use.
BusinessMCP does that for you. Connect your tools once and you get one hosted MCP endpoint that exposes them as clean, namespaced, permission-scoped tools. Instead of writing and maintaining a send_email or get_analytics tool per system, you point your agent at your endpoint with a Bearer mcph_* key and it discovers them automatically — the same way any MCP client does. See Expose your MCP endpoint and Connect Claude, GPT & Gemini.
If you do want to hand-build a server, How to build an MCP server covers that path.
Next steps
You now have the shape of an agent: a loop, tools, and safety. From here, deepen it with real planning, memory and monitoring — and read AI agent best practices before you put it in front of users. To ground the concepts, revisit What are AI agents? and What is MCP?.
Frequently asked questions
What is the difference between building an AI agent and an MCP server?
An MCP server exposes tools and data; an AI agent is the intelligent system that decides which tools to call to reach a goal. You often build both — but with BusinessMCP the server side is handled, so you can point an agent at a ready-made endpoint instead.
How do I make the agent safe and reliable?
Start with limited permissions, require human approval for consequential actions, log every tool call, and add rate limits and a cost cap. Expand what the agent can do only as it proves itself in lower-risk tasks.
Keep going
Turn your company into one AI-ready data platform on a single hosted MCP endpoint.