Skip to main content

10. Agent Communication Protocols

Building and Integrating MCP with AI Agents

Building and integrating MCP with AI agents means wrapping real backends as MCP servers, connecting them through a host MCP client, mapping discovered tools into the model’s tool API, then running the usual plan–act–observe loop with allowlists, HITL, and budgets. MCP is the plug; the agent framework is still the planner. Ship read-only tools first, test list/call paths, then add writes behind gates.

What you will learn

  • Build a thin MCP server over an existing API
  • Wire MCP clients into an agent tool loop
  • Map, filter, and invoke tools safely under policy
  • Test, package, and operate MCP integrations
  • Answer interview questions with short, solid answers
Agent loop host policy MCP client servers
Interview staple: agents plan; MCP clients execute discovered tools under host policy.

Definition

Integrating MCP with agents is the practice of exposing capabilities via MCP servers and consuming them inside an agent host: discover tools/resources, map them to the LLM’s function/tool interface, invoke through the MCP client when the model requests a call, then feed structured results back into the loop.

Building an MCP server is usually a thin, well-schema’d adapter — not a rewrite of your business systems.

Introduction

This lesson (#4 in Agent Communication Protocols) follows MCP Architecture. You know clients vs servers; now build and wire them into real agent loops.

Next: Agent-to-Agent (A2A) Protocol — when peers, not just tools, must collaborate.

Integration goals

  • Reuse one server across IDE, chat, and batch agents
  • Keep backend credentials inside the server boundary
  • Let agents discover new tools without redeploying hosts
  • Enforce allowlists and HITL in the host
  • Trace every MCP call for eval and audit

Build a server

Start small: one backend, few tools, clear schemas.

Wrap define validate test connect MCP server build
Read-only tools first; writes only after tests and HITL design.
  • Wrap API - scoped service account, timeouts, retries
  • Define - tool names, descriptions, JSON Schema args
  • Validate - reject bad args; return actionable errors
  • Test - list capabilities and call happy/error paths
  • Package - stdio for local hosts or HTTP for shared
  • Document - risk level per tool (read vs write)

Agent loop over MCP

The planning loop stays familiar; only the tool backend changes.

Discover map decide call MCP observe loop
Treat MCP results like any tool output: untrusted and budgeted.
  • Discover - client lists tools/resources from attached servers
  • Map - convert allowed tools to the model tool schema
  • Decide - model selects a tool call (or user runs a prompt)
  • Call - host may HITL, then MCP client invokes the server
  • Observe - parse result into messages; continue or stop
  • Budget - max tool calls, tokens, and wall-clock

Resources and prompts in agents

  • Pull resources into context when the task needs grounded docs
  • Use prompts to start standardized workflows from the UX
  • Do not auto-dump every resource every turn
  • Cache stable resources; refresh volatile ones deliberately

Multi-server and frameworks

  • Attach specialized servers (repo, CRM, browser) per agent role
  • Namespace tool names in the host if collisions appear
  • Framework adapters (custom runners, LangGraph, etc.) still need a real MCP client
  • Prefer official/SDK clients over hand-rolled JSON-RPC when available
  • Isolate failing servers so one outage does not halt all tools

Safety checklist

  • Consent before enabling a new server in the host
  • Default-deny write tools; HITL for email, money, prod changes
  • Redact secrets in traces and model-visible errors
  • Sanitize/segment untrusted tool output against injection
  • Rotate credentials; never bake prod keys into prompts

Failure modes

  • Exposing raw admin APIs as tools without scopes
  • Skipping schema validation on the server
  • Mapping every discovered tool with no allowlist
  • No timeouts — hung backends stall the agent
  • Testing only in a demo host, never in CI list/call checks

Eval and ops

  • Golden fixtures: list tools + expected call/response pairs
  • Shadow new servers before broad agent rollout
  • Monitor error rates, latency, and HITL reject reasons
  • Version servers; pin compatible protocol versions in hosts

Program / implementation notes

Implement a minimal server with search_kb (read) and draft_reply (write, HITL). Connect via host client; run an agent loop that may only auto-call search_kb. Log correlation ids across agent step and MCP call.

  • Ship the read path to production agents first
  • Next: Agent-to-Agent (A2A) Protocol

Advantages and limitations

  • Advantage: one server powers many agent hosts
  • Advantage: cleaner credential and schema boundaries
  • Advantage: faster tool onboarding via discovery
  • Limitation: host client quality varies by product
  • Limitation: bad schemas still produce bad agent behavior
  • Limitation: remote transports add latency and auth complexity

Key points

  • Build thin MCP servers: wrap API → schema → validate → test → connect.
  • Agent loop: discover → map → decide → call MCP → observe.
  • Keep allowlists and HITL in the host, not only the server.
  • Start read-only; gate writes.
  • Test list/call in CI; monitor latency and errors in ops.
  • MCP backs tools; the agent framework still plans.

FAQs

Do I need MCP to build an agent?

No. Agents can use local function tools. MCP helps when you want reusable, discoverable servers across multiple hosts and teams.

Where does HITL sit in an MCP agent?

In the host, before the MCP client invokes a high-risk tool — after the model proposes the call, before side effects run.

How do frameworks use MCP?

They attach an MCP client (or adapter), convert allowed MCP tools into the framework’s tool objects, and route executions through the client.

What should the first production MCP tool be?

Usually a read-only, low-risk capability with a tight schema — search, fetch record, or get status — before any write tools.

Interview questions

  1. How do you integrate MCP with an AI agent?

    Answer Attach MCP servers via a host client, discover and allowlist tools, map them to the model tool API, and invoke through the client inside the agent’s plan–act–observe loop.

  2. What are the steps to build an MCP server?

    Answer Wrap a backend with scoped creds, define tool/resource schemas, validate args, test list/call paths, package for a transport, and document risk levels.

  3. Where is policy enforced?

    Answer Primarily in the host/client: consent, allowlists, HITL, and budgets. Servers still validate and least-privilege their backends.

  4. How does the agent tool loop use MCP?

    Answer Discover tools, map to LLM tools, let the model decide a call, optionally HITL, execute via MCP client, observe the result, and repeat under budgets.

  5. How do you test MCP integrations?

    Answer Automate list and call fixtures in CI, cover error paths and timeouts, then shadow in a staging host before wide agent rollout.

  6. MCP server vs putting tools inside the agent process?

    Answer In-process tools are fine for one app. MCP servers isolate credentials, enable reuse across hosts, and standardize discovery — at the cost of another process/service.

Related topics

Practice task

Complete each numbered practice step in a notebook or doc before opening the next lesson.

  1. Pick one internal API and list 2 read tools and 1 write tool for an MCP server.
  2. Write allowlist + HITL rules for those three tools in an agent host.
  3. Sketch the discover→map→decide→call→observe sequence for a sample user task.
  4. Define three CI checks (list tools, happy call, error/timeout).
  5. Answer interview questions 1, 3, and 4 in two sentences each.