Platform: Z.aiAPI guide
Independent developer guide

MCP Tool Calling API Guide for AI Applications

Model Context Protocol (MCP) can connect a model-driven application to external tools such as search, document processing, or data services. This guide explains the application pattern and the checks needed before exposing tools to a model.

What this workflow helps you do

Z.ai documents MCP server calling through its chat-completions interface, including server labels, server URLs, transport choices, optional headers, and allowed tool names. The model can request a tool call, but your application still needs to decide what it is allowed to do and how the result is validated.

Practical steps

Map the tool boundary

Choose one clear capability, such as searching an approved document index. Record the MCP server address, authentication method, allowed operations, data classification, and expected response format. Keep credentials on the server side and avoid giving a model access to broad or unrelated tools.

Restrict available tools

Use an allowlist for the tools relevant to the current task. The official API guide documents SSE and Streamable HTTP transport options; use the transport required by the MCP server and configure its authentication headers securely. If a tool can send messages, change records, or spend money, add a separate authorization or confirmation step.

Validate tool requests and results

Treat tool arguments as untrusted input. Validate types, ranges, resource identifiers, and user permissions before executing an operation. Validate the returned data before including it in the next model request. Add timeouts, request limits, audit logging, and clear failure handling for unavailable MCP servers.

Test the whole call loop

Test normal responses, no-result responses, malformed arguments, permission denial, server timeout, and repeated calls. Confirm the model can explain when it cannot use a tool. Monitor external-service access separately from model API usage so that unexpected tool calls are visible.

Detailed implementation guidance

Design tool descriptions for reliable selection

Give each MCP tool a clear name, short purpose, and precise input schema. Explain when it should be called and what it must never do. Avoid two tools with nearly identical descriptions if the model must choose between them. Keep the output concise and structured so the model can use the result without guessing which fields are authoritative.

Separate read actions from write actions

Search and lookup tools usually have a different risk profile from tools that modify records, publish content, or contact people. Use separate tool permissions and require application-side confirmation for consequential writes. Validate that the signed-in user can access the target resource, even when the model supplied its identifier. Do not rely on prompt wording as an authorization boundary.

Operate MCP connections reliably

Track tool-call volume, failure reasons, latency, and server availability. Set a maximum number of tool calls per task to prevent loops, and define how the agent should respond when a tool times out. Review audit logs for unexpected operations while protecting credentials and sensitive returned content. A useful MCP integration makes its boundaries observable to operators.

Example request structure

Use placeholders for credentials and confirm the exact schema against the provider documentation before adopting an example.

{
  "model": "glm-5.3",
  "messages": [{"role": "user", "content": "Search the approved knowledge base for the deployment guide."}],
  "tools": [{
    "type": "mcp",
    "mcp": {
      "server_label": "knowledge-search",
      "server_url": "https://mcp.example.com/mcp",
      "transport_type": "streamable-http",
      "allowed_tools": ["search_documents"]
    }
  }],
  "tool_choice": "auto"
}

Quality and safety checks

Review generated output, protect secrets and private data, and run the checks your project requires. Model responses and tool calls should be treated as proposals until your application or a developer validates them.

Common questions about mcp tool calling api guide for ai applications

What is the difference between MCP and ordinary function calling?

Both can connect model output to external actions. MCP provides a standard way to connect to tool servers and their resources, while function calling often describes tools declared directly by an application. Choose based on your architecture and provider support.

Should I let a model call every tool on an MCP server?

No. Limit tools to the current task and enforce authorization in your application. A model-generated request is not permission to access or change a resource.

Related guides

Official references: MCP server calling API documentation · API quick start and request formats · Confirm current model access, pricing, limits, and API behavior in provider documentation.