Advanced Generative AI Development: Understanding the MCP Protocol and Tool Calling in Practice

    This article explains why tool calling is essential for large language models, how the Model Context Protocol (MCP) differs from traditional Function Calling, and how MCP's client-server architecture enables dynamic tool discovery. It walks through a weather query example, demonstrates how to build a minimal MCP server with Python, and offers engineering practices covering tool descriptions, input validation, permissions, server granularity, and audit logging. Readers will gain a clear mental model of MCP and practical guidance for adopting it in production AI applications.

    Why MCP Is Needed

    A large language model cannot directly access databases, call APIs, or read local files. To give a model the ability to “act,” you need a tool-calling mechanism. Early Function Calling (FC) solved the basic problem, but it is essentially a “developer hand-wiring” model: every time you add a new tool, you must define the function signature in code, write the schema description, capture the model’s returned call request, execute the local function, and stitch the result back into the context. This entire pipeline is hardcoded on the client side by developers.

    The Model Context Protocol (MCP) was introduced to break out of this point-to-point integration dilemma. Proposed by a major AI company, its goal is to standardize how AI interacts with external tools and data sources. You can think of MCP as a USB-C interface: as long as a tool exposes its capabilities according to the protocol, any AI application that supports the protocol can discover and use it in a plug-and-play manner, without writing custom integration code for each tool.

    The Essential Difference Between MCP and Function Calling

    The two are not mutually exclusive; they are solutions at different levels of abstraction. Understanding their differences helps you choose the right engineering path.

    1. How tools are defined

    • FC: The tool schema lives in your application code, and the model-to-tool relationship is statically bound. Adding a tool means modifying code and redeploying.
    • MCP: Tools are declared by independent MCP servers. The client dynamically pulls the tool list and calling specifications from the server at runtime, so the set of tools the model “sees” can change over time.

    2. Division of responsibilities

    • In FC, the application’s orchestration logic, parameter validation, and result handling are all coupled together.
    • In MCP, tool implementations are pushed down into server processes, while the client only handles protocol communication and result forwarding. Responsibilities are clean, and each side can iterate independently.

    3. Ecosystem reusability

    Tools written for FC only serve your own application. An MCP-compliant server, by contrast, can be reused by any client that supports the protocol, forming a genuine “tool ecosystem” — build once, plug in everywhere.

    An Example: Querying Beijing’s Weather

    Using a weather query, compare the cost of the two approaches.

    FC workflow:

    1. 1. Define the get_weather(city, date) function and its JSON schema in code;
    2. 2. Send the schema to the model with the request;
    3. 3. The model returns tool_calls; you manually parse and execute the local function;
    4. 4. Fill the result back into the context as a tool message and request the model
    5. again to generate the final answer.

    Every step requires glue code from the developer, and any failure must be handled manually.

    MCP workflow:

    1. 1. Start (or reuse an existing) MCP weather server that declares the weather tool
    2. according to the protocol;
    3. 2. The AI client automatically discovers the server and its tool list through the
    4. protocol;
    5. 3. When the model decides to call the tool, the client forwards the request via
    6. the protocol; the server executes it and returns the result;
    7. 4. The result is automatically filled back into the context, and the model produces
    8. the final answer.

    Developers no longer write invocation logic; they only maintain a protocol-compliant server.

    Core MCP Architecture

    MCP uses a client-server architecture with three roles:

    • MCP Host: The application running the model, such as an agent framework, an IDE plugin, or a chat client.
    • MCP Client: The connector embedded in the host, responsible for establishing sessions with servers, negotiating capabilities, and forwarding requests.
    • MCP Server: Exists as a process or service and exposes three core capabilities: Tools (callable tools), Resources (readable data resources), and Prompts (predefined prompt templates).

    Communication between them typically uses JSON-RPC-style messages. The server reports the capabilities it offers to the client; the client updates the tool descriptions it can pass to the model accordingly. When the model decides to call a tool, the client forwards the parameters to the server through the protocol and waits for the execution result.

    There are two common transport mechanisms:

    1. Standard input/output: The server runs as a local subprocess. This suits scenarios that access local files or databases, keeping data inside the internal network.
    2. Streaming HTTP: The server is deployed as a remote service. This suits scenarios where teams share tools or multiple users need access.

    Building a Minimal MCP Server

    Here is an example using a lightweight Python SDK commonly used in the ecosystem (for illustration only; refer to the specific SDK documentation you use):

    1. from mcp.server.fastmcp import FastMCP
    2. mcp = FastMCP("weather")
    3. @mcp.tool()
    4. def get_weather(city: str, date: str = "today") -> str:
    5. """Query the weather for a given city on a given date."""
    6. # In a real project, call a weather API or an internal data service here
    7. return f"{city} {date}: cloudy, 26°C, southeast wind force 3"
    8. if __name__ == "__main__":
    9. mcp.run()

    Key points:

    • The @mcp.tool() decorator makes the SDK automatically generate a protocol-compliant tool description from the function signature and docstring, including the name, parameter schema, and usage description — exactly what the model relies on to decide “whether and how to call” the tool.
    • Once the server starts, clients can dynamically discover the get_weather tool. The application code never needs to know the tool’s details.

    Engineering Practice Recommendations

    1. Tool descriptions are the model’s “manual.” Parameter naming and docstring wording directly affect calling accuracy. Use clear, unambiguous natural language to describe the purpose and meaning of each parameter.

    2. Perform strict input validation and error handling. Parameters supplied by the model may be invalid. The server should return structured error information so the model can understand the failure and self-correct.

    3. Apply the principle of least privilege. Once an MCP server is connected, its capabilities are effectively opened up to the model. For operations involving the file system, databases, payments, or other sensitive actions, be sure to configure access scopes, read-only modes, or manual confirmation steps.

    4. Split servers at a sensible granularity. Divide servers by domain (for example, database tools, retrieval tools, notification tools) rather than cramming every tool into one large server. This improves maintainability and access control.

    5. Keep audit logs. Record the request parameters and execution results of every tool call for troubleshooting and security auditing. Combined with logging services and monitoring/alerting systems, this keeps the tool chain reliably operational.

    Conclusion

    MCP upgrades tool calling from “hand-written glue code” to a “protocol-based, ecosystem-oriented” integration approach, giving AI applications the ability to dynamically discover and plug in external capabilities. For developers, mastering MCP means not only less boilerplate code but also the opportunity to build agents on top of an open tool ecosystem. In practice, you can start with a minimal server and progressively protocolize internal capabilities such as data queries, document retrieval, and message notifications, turning the model into an intelligent hub capable of orchestrating all kinds of capabilities.