Ch 4 — Tools & Function Calling

Inside the @tool decorator, bind_tools serialization, ToolMessage protocol, and MCP internals
Under the Hood
-
Click play or press Space to begin...
Step- / 10
A@tool Decorator InternalsWhat happens when Python loads your function
1
code
Your Functiondef + type hints
+ docstring
inspect
schema
Pydantic SchemaAuto-generated
args_schema model
wrap
2
build
StructuredToolBaseTool subclass
name, desc, schema
3
arrow_downward bind_tools: how tool schemas reach the LLM API
Bbind_tools & API SerializationConverting tools into the format the LLM API expects
build
StructuredTool.name .description
.args_schema
convert
data_object
JSON SchemaOpenAI "function"
format dict
attach
4
send
HTTP Requesttools=[ ] in
API payload
5
arrow_downward API response parsing: tool_calls on AIMessage
CResponse Parsing & AIMessage.tool_callsHow the raw API response becomes structured tool_calls
cloud_download
API ResponseJSON with
tool_calls array
parse
psychology
AIMessage.content = ""
.tool_calls = [...]
extract
6
list_alt
ToolCall Dictname, args (parsed),
id, type
7
arrow_downward Tool execution and ToolMessage construction
DTool Execution & ToolMessageRunning the function and packaging the result
list_alt
ToolCall Dictname + args
from AIMessage
lookup
play_arrow
Execute Fntool.invoke(args)
runs your code
wrap
mail
ToolMessagecontent + id
→ back to LLM
8
arrow_downward ToolNode: LangGraph's automatic tool executor
EToolNode in LangGraphAutomatic tool dispatch — the bridge between graph and tools
psychology
Agent NodeLLM returns
AIMessage
has tools?
call_split
Conditional Edgetool_calls?
→ tools : END
yes
build
ToolNodeAuto-dispatches
all tool_calls
loop
9
replay
Back to AgentToolMessages
added to state
10
arrow_downward MCP protocol internals: JSON-RPC, transports, discovery
FMCP Protocol InternalsJSON-RPC messages, transports, and tool discovery
smart_toy
MCP Clientinitialize →
tools/list
JSON-RPC
hub
MCP Servertools/call →
execute + return
transport
swap_horiz
stdio / SSELocal process or
remote HTTP
1
Detail