Lesson 16 / 31

Using MCP Tools Inside Your Own Agent

Bridge MCP tool definitions into an agent loop.

List, convert, call, return

When you build your own host or agent, the loop is: connect and initialise; tools/list; convert each MCP tool (name, description, inputSchema) into your model API's tool format (the JSON Schema usually maps directly); send the user message with those tools; when the model returns a tool call, run policy checks, call tools/call on the right session, convert the content result back into a tool-result message and continue until the model answers. Many agent frameworks (LangChain, LangGraph, the OpenAI and Anthropic agent SDKs and others) ship MCP adapters that do this conversion, but the responsibilities remain yours: approvals, limits, logging and error handling.

The agent-side loop (pseudocode)

Frameworks wrap this; knowing it helps with debugging. Illustrative; not run here.

tools = mcp_session.list_tools()                       # tools/list (follow pagination)
model_tools = [{"name": t.name, "description": t.description,
                "input_schema": t.inputSchema} for t in tools]

messages = [user_message]
for step in range(MAX_STEPS):                                # always cap the loop
    reply = model.call(messages, tools=model_tools)
    if not reply.tool_calls:
        return reply.text
    for call in reply.tool_calls:
        ok, why = authorize(call.name, call.args)             # your policy / approval layer
        result = mcp_session.call_tool(call.name, call.args) if ok else {"error": why}
        messages.append(tool_result_message(call.id, result))

Quick check: What do MCP adapters in agent frameworks NOT do for you?

  • List tools
  • Convert tool schemas
  • Decide your approval policy, limits and logging
  • Forward calls to a server
Answer

Decide your approval policy, limits and logging — Safety and governance remain the application's responsibility.