Lompat ke konten

Pelajaran 11 ยท Tool Calling and Agents

Built-In Tools and Remote MCP

Use OpenAI's hosted tools such as web search and file search, connect remote MCP servers with approvals, show citations and manage cost and risk.

With function calling, you write and run every tool. The Responses API also offers built-in tools that run on OpenAI's side, and it can connect to tools on remote MCP servers. You turn them on by adding entries to the tools list. They save a lot of work, and they also widen what can go wrong, so choose them deliberately.

The tool landscape

OpenAI's tools guide covers several kinds. As of December 2026 they include the following.

Tool What it does
Web search (web_search) Finds current information on the web
File search (file_search) Searches files in your vector stores
Remote MCP (mcp) Calls tools offered by an MCP server
Tool search (tool_search) Loads tool definitions only when needed
Others Shell, computer use, image generation, skills and more

Which models support which tool can differ, and some tools have extra setup or fees. Check the tools guide for your model.

Python
# Read-only
response = client.responses.create(
    model="gpt-6.2-sol",
    tools=[{"type": "web_search"}],
    input="What changed in the latest Python release? Cite sources.",
)
print(response.output_text)

The model decides whether to search. The output can include a web_search_call item and then a message. The text part of the message carries annotations of type url_citation with the address, title and position of each cited source.

OpenAI requires that when you show web search results to users, citations are visible and clickable. You can also restrict results to trusted sites with filters.allowed_domains:

Python
tools=[{"type": "web_search",
        "filters": {"allowed_domains": ["python.org", "docs.python.org"]}}]

As of December 2026, the pricing page lists web search at $10.00 per 1,000 calls, plus the search content tokens at model rates.

A citation shows where a claim came from, not that it is right. Pages can be outdated or false, so check important facts at the source.

Show citations in your interface

Your program has to turn annotations into links. This stand-in uses an invented annotation list on invented text, with a placeholder address.

Python Stand-in: turn citation annotations into links

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The helper escapes all text and accepts only https links. In the real API, check the annotation fields and index behavior in the current reference before you build on them.

Remote MCP servers

The Model Context Protocol (MCP) is an open way to offer tools to AI applications. You can learn the idea in Model Context Protocol. With the Responses API, you point to a server:

Python
# Read-only: the server address is a placeholder
response = client.responses.create(
    model="gpt-6.2-sol",
    tools=[{
        "type": "mcp",
        "server_label": "inventory",
        "server_url": "https://mcp.example.com/mcp",
        "allowed_tools": ["search_stock"],
        "require_approval": "always",
    }],
    input="Do we have 20 blue chairs in stock?",
)

Important fields:

  • allowed_tools imports only the tools you name. Fewer tools means less risk and fewer tokens.
  • require_approval sets the approval rule. Approval is required by default.
  • authorization carries an access token when the server needs one. The API does not store it, so you send it with each request.

The approval flow

When approval is needed, the output contains an mcp_approval_request item with the tool name, arguments and server label. Show these to a person or apply a strict policy. Then send a new request with an mcp_approval_response:

Python
# Read-only
approval = next(i for i in response.output if i.type == "mcp_approval_request")

follow_up = client.responses.create(
    model="gpt-6.2-sol",
    previous_response_id=response.id,
    tools=tools,   # the same MCP tool list
    input=[{"type": "mcp_approval_response",
            "approval_request_id": approval.id,
            "approve": True}],
)

Each approval covers one call. Do not set require_approval to "never" just to remove friction. If you do skip approval, do it only for named read-only tools on a server you trust.

Risks to take seriously

  • Trust: OpenAI does not verify remote MCP servers. Prefer servers run by the service itself.
  • Data sharing: Arguments sent to a server are governed by that server's policies.
  • Prompt injection: Web pages, files and server results can contain hidden instructions.
  • Change: A server can change what its tools do without telling you.
  • Leaks through links: Opening a URL from tool output can send data to its owner.

Combining a tool that reads private data with a tool that can send data out is especially risky. Think about what an attacker could do if the model followed their text.

Choose hosted or your own

Choose a built-in tool when Choose your own function when
The standard behavior is enough You need exact control and logging
You want less infrastructure Data must stay inside your system
The extra fee is acceptable You need custom permission checks

You can mix both. Give each request only the tools it needs. A support bot that answers policy questions does not need a shell.

Log each tool call with its name, a safe summary of the arguments, the approval decision and the time. You will need this when a user asks why the app did something.

Watch cost and delay

A built-in tool can turn one request into several steps. Search results are added to the model's input, which costs tokens, and each remote call adds waiting time. Tool definitions imported from an MCP server also take space in the context. Measure a typical request with and without each tool. If a tool is rarely useful for a feature, remove it from that feature. For web search, decide what your app should say when nothing reliable is found, and test that case instead of hoping for a good answer.

Also plan for a tool that is down. A clear message such as "I could not search right now" is far better than an answer that silently relies on old knowledge.

Common mistakes

  • Turning on every tool by default because it might be useful some day.
  • Showing the text of a web answer but throwing away its citation links.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must you do when showing web search results to users?

    OpenAI requires visible and clickable inline citations for web results.
  2. 2. What is the default for remote MCP tool calls?

    You must explicitly relax the approval policy if you choose to.
  3. 3. What does allowed_tools do?

    Importing fewer tools reduces both risk and token use.
  4. 4. Does a citation prove that a statement is true?

    Sources can be wrong or out of date, and a summary can misread them.

Summary

  • Add built-in tools to the tools list, and check model support and fees.
  • Web search returns citation annotations that you must show as links.
  • Remote MCP tools require approval by default; limit them with allowed_tools.
  • Treat servers and tool results as untrusted.
  • Give each request only the tools it needs.

Next: The Agents SDK

Progres Anda disimpan di browser ini. Tidak perlu akun.