AI Agent Development and Interview Guide: 05-Tool Calling and MCP Protocols in Practice

Analyzes JSON Schema constraints and side-effect isolation in tool calling, builds standardized MCP (Model Context Protocol) servers, and handles security and idempotency.

Contents26 sections

Chapter 5: Tool Calling technical system component MCP

Tool technical system component“technical system component”technical system component“technical system component、technical system component”。technical system component,Tool technical system component Agent technical system component。technical system component Tool technical system component,technical system component Schema、technical system component、technical system component、technical system component、technical system component。

1. Tool technical system component

technical system component、Shell technical system component HTTP technical system component。technical system component:

TEXT
technical system component:execute_sql(sql: string)
technical system component:get_order_status(order_id: string)

technical system component:http_request(url, method, body)
technical system component:create_refund_request(order_id, reason, amount)

technical system component:

  • technical system component;
  • technical system component;
  • technical system component;
  • technical system component;
  • technical system component Prompt Injection technical system component。

🔥 P0 High-Frequency Must-Know: technical system component Agent technical system component SQL technical system component?
technical system component SQL technical system component,technical system component、technical system component。technical system component;technical system component SQL technical system component、technical system component/technical system component、technical system component、technical system component、technical system component。

2. technical system component Tool Schema technical system component

  1. technical system component;
  2. technical system component“technical system component”technical system component“technical system component”;
  3. technical system component、technical system component、technical system component;
  4. technical system component;
  5. technical system component;
  6. technical system component。
PYTHON
from decimal import Decimal
from typing import Literal

from pydantic import BaseModel, Field


class RefundRequest(BaseModel):
    order_id: str = Field(pattern=r"^ORD-[0-9]{8}$")
    reason: Literal["duplicate_charge", "not_received", "quality_issue", "other"]
    amount: Decimal = Field(gt=0, max_digits=10, decimal_places=2)
    idempotency_key: str = Field(min_length=16, max_length=128)


class RefundResult(BaseModel):
    request_id: str
    status: Literal["pending_review", "approved", "rejected"]
    message: str

technical system component;technical system component“technical system component”;technical system component,technical system component。

3. Tool technical system component

technical system component:

PYTHON
from typing import Literal
from pydantic import BaseModel


class ToolError(BaseModel):
    code: Literal[
        "NOT_FOUND",
        "PERMISSION_DENIED",
        "INVALID_STATE",
        "RATE_LIMITED",
        "TEMPORARY_UNAVAILABLE",
    ]
    retryable: bool
    user_fixable: bool
    message: str
    missing_fields: list[str] = []

Agent technical system component retryable technical system component,technical system component user_fixable technical system component;PERMISSION_DENIED technical system component,technical system component“technical system component”。

4. Tool Calling technical system component

technical system component SDK technical system component,technical system component:

PYTHON
async def run_tool_loop(model, tool_registry, messages, max_steps=6):
    for step in range(max_steps):
        response = await model.respond(
            messages=messages,
            tools=tool_registry.schemas(),
        )

        if response.final_text is not None:
            return response.final_text

        for call in response.tool_calls:
            tool = tool_registry.get(call.name)

#            # 1. technical system component
            if tool is None:
                result = {"error": {"code": "UNKNOWN_TOOL", "retryable": False}}
            else:
#                # 2. Schema technical system component
                args = tool.input_model.model_validate(call.arguments)

#                # 3. technical system component
                decision = await authorize_tool(tool, args)
                if decision.requires_approval:
                    return await pause_for_approval(call, decision)

#                # 4. technical system component、technical system component、technical system component
                result = await tool_executor.execute(tool, args)

#            # 5. technical system component call_id technical system component
            messages.append({
                "role": "tool",
                "tool_call_id": call.id,
                "content": result,
            })

    raise RuntimeError("agent exceeded maximum tool steps")

technical system component。technical system component、technical system component、technical system component。

🔥 P0 High-Frequency Must-Know: technical system component Tool technical system component?
technical system component;technical system component Schema;technical system component;technical system component;technical system component;technical system component;technical system component/technical system component;technical system component;technical system component。

5. technical system component

technical system componenttechnical system componenttechnical system component
R0 technical system componenttechnical system component、technical system componenttechnical system component、technical system component、technical system component
R1 technical system componenttechnical system component、technical system componenttechnical system component、technical system component、technical system component、technical system component
R2 technical system componenttechnical system component、technical system componenttechnical system component、technical system component、technical system component
R3 technical system componenttechnical system component、technical system component、technical system componenttechnical system component、technical system component、technical system component
R4 technical system component/technical system componenttechnical system component、technical system component、technical system componenttechnical system component、technical system component、technical system component

technical system component Tool technical system component,technical system component。

6. technical system component、technical system component

technical system componentMechanism:

  • technical system component:technical system component;
  • technical system component:technical system component;
  • technical system component:technical system component,technical system component。

technical system component“technical system component”technical system component,technical system component。technical system component:technical system component、technical system component、technical system component、technical system component;technical system component,technical system component。

⭐ P1:Saga technical system component Agent technical system component?
Agent technical system component。technical system component Saga technical system component;technical system component,technical system component。

7. technical system component MCP

Model Context Protocol(MCP)technical system component AI technical system component。technical system component 2025-11-25 technical system component。MCP technical system component JSON-RPC,technical system component JSON Schema technical system component。

MCP Server technical system component:

technical system componenttechnical system componenttechnical system component
Toolstechnical system componenttechnical system component
Resourcestechnical system componenttechnical system component,technical system component、technical system component Schema、technical system component
Promptstechnical system componenttechnical system component

technical system component Understanding MCP servers。

🔥 P0 High-Frequency Must-Know: MCP technical system component Function Calling technical system component?
Function Calling technical system component;MCP technical system component Server technical system component Tools、Resources、Prompts technical system component。MCP technical system component,technical system component、technical system component。

8. MCP technical system component

TEXT
technical system component
  -> Host(AI technical system component)
      -> MCP Client A -> MCP Server A(knowledge bases)
      -> MCP Client B -> MCP Server B(technical system component)
      -> MCP Client C -> MCP Server C(technical system component)
  • Host technical system component、technical system component、technical system component;
  • Client technical system component Server technical system component、technical system component;
  • Server technical system component Tools/Resources/Prompts;
  • Server technical system component Server technical system component。

9. technical system component MCP Server

technical system component Python technical system component FastMCP,technical system component Docstring technical system component。technical system component Build an MCP server。

PYTHON
from typing import Literal

from mcp.server.fastmcp import FastMCP
from pydantic import Field


mcp = FastMCP("support-tools")


@mcp.tool()
async def get_ticket_status(
    ticket_id: str = Field(pattern=r"^T-[0-9]{6}$"),
) -> dict:
    """technical system component。technical system component,technical system component。"""
#    # technical system component user_id,technical system component。
    return {
        "ticket_id": ticket_id,
        "status": "open",
        "updated_at": "2026-07-22T10:00:00Z",
    }


@mcp.tool()
async def create_ticket_draft(
    title: str = Field(min_length=5, max_length=120),
    category: Literal["billing", "technical", "other"] = "other",
) -> dict:
    """technical system component,technical system component;technical system component。"""
    return {
        "draft_id": "D-000001",
        "title": title,
        "category": category,
        "status": "draft",
        "requires_user_confirmation": True,
    }


if __name__ == "__main__":
    mcp.run(transport="stdio")

technical system component SDK technical system component,technical system component:

BASH
uv add "mcp[cli]"

STDIO technical system component

STDIO technical system component,technical system component。technical system component:technical system component print() technical system component stdout,technical system component JSON-RPC。technical system component stderr technical system component。

PYTHON
import logging
import sys

logging.basicConfig(stream=sys.stderr, level=logging.INFO)

technical system component MCP technical system component。

10. Resources technical system component Tools technical system component

technical system component Resource:

  • technical system component;
  • Host technical system component;
  • technical system component、Schema、technical system component;
  • technical system component“technical system component”。

technical system component Tool:

  • technical system component;
  • technical system component;
  • technical system component;
  • technical system component。

technical system component Tool technical system component。technical system component Resource URI technical system component Tool technical system component,technical system component。

11. MCP technical system component

technical system component HTTP technical system component,2025-11-25 technical system component OAuth 2.1 technical system component Protected Resource Metadata。technical system component:

  • Access Token technical system component;
  • Server technical system component Token Audience;
  • technical system component Token technical system component;
  • technical system component Scope;
  • HTTPS、PKCE、technical system component Token、technical system component;
  • technical system component Token、technical system component。

technical system component MCP Authorization。STDIO technical system component,technical system component HTTP OAuth technical system component。

🔥 P0 High-Frequency Must-Know: technical system component Token Passthrough technical system component?
technical system component Token,technical system component confused deputy。MCP Server technical system component Token technical system component;technical system component、technical system component。

12. Tool technical system component

MCP Server technical system component Tool technical system component。Host technical system component Server technical system component。technical system component:

  • Server technical system component/technical system component;
  • technical system component Server technical system component Tool technical system component;
  • technical system component;
  • Tool technical system component Schema technical system component;
  • technical system component;
  • technical system component、technical system component;
  • technical system component Server technical system component。

13. Tool technical system component

technical system component:

  1. technical system component:technical system component;
  2. technical system component:technical system component、technical system component、technical system component;
  3. technical system component:technical system component、technical system component、technical system component、technical system component;
  4. technical system component:technical system component、technical system component。

technical system component:

JSON
{
  "input": "technical system component T-123456 technical system component",
  "expected_tool": "get_ticket_status",
  "expected_arguments": {"ticket_id": "T-123456"},
  "forbidden_tools": ["create_ticket_draft"],
  "max_tool_calls": 1
}

14. technical system component

technical system component A:technical system component

technical system component Tool Schema、technical system component、technical system component Scope、technical system component Registry。technical system component。

technical system component B:MCP Server

technical system component:technical system component Tool、technical system component Tool、technical system component Resource。STDIO technical system component stderr。

technical system component C:technical system component

testing:technical system component Tool、technical system component、technical system component、technical system component Tool technical system component、technical system component、technical system component。

15. technical system component

🔥 P0:technical system component Tool?

technical system component、technical system component/technical system component Schema、technical system component、technical system component、technical system component、technical system component、technical system component、technical system component、technical system component、technical system component。technical system component、technical system component。

🔥 P0:MCP technical system component Tools、Resources、Prompts technical system component?

Tools technical system component;Resources technical system component;Prompts technical system component。technical system component,technical system component Tool。

🔥 P0:Tool technical system component?

technical system component:technical system component;technical system component;technical system component;technical system component;technical system component,technical system component。

⭐ P1:MCP technical system component?

technical system component、technical system component,technical system component Server technical system component、Tool technical system component、technical system component、technical system component、technical system component Host/Server technical system component。technical system component。

⭐ P1:technical system component Tool technical system component?

technical system component;technical system component Tools;technical system component;technical system component;technical system component;technical system component。

16. technical system component

  • technical system component、technical system component、technical system component;
  • technical system component—technical system component—technical system component—technical system component—technical system component—technical system component;
  • technical system component MCP Host/Client/Server technical system component;
  • technical system component MCP Server;
  • technical system component HTTP technical system component、Token Audience technical system component;
  • technical system component、technical system component、technical system component。

REFERENCES

References

  1. 012025-11-25 规范
  2. 02Understanding MCP servers
  3. 03Build an MCP server
  4. 04MCP Authorization

Series

AI Agent Development and Interview Guide

Next step

Continue with related topics

Continue along the same topic.

Browse latest news