MCP servers

Tool Discovery

Protocol version, capability, identify

Model Decision

Choose and call server with specific schema

Server Response

Provide information or perform actions

Introduction

Model Context Protocol (MCP) is an open-source standard for connecting AI applications to external systems. It adopts Client and Server architecture with specified protocol for information exchange. As a universal protocal, many AI applications support it, including AI agents (e.g., Claude Code Desktop), AI chatbots (e.g., chatgpt.com), agentic IDE (e.g., Cursor), etc.

A MCP server is a bridge program that connects AI agents to external data sources, tools, and systems using the MCP protocol, enabling them to access information and perform actions.

For more information, please visit https://modelcontextprotocol.io/ to learn more.

Interaction model

Note that even though the MCP servers are availiable, it is the AI's decisions whether to call the servers or not.

The MCP server can be hosted remotely and connected via network.

Scaffold code

A minimal implementation of MCP server with FastMCP.

mcp_server_template.py
python
from typing import Annotated
from pydantic import Field
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("scaffold")

@mcp.tool()
def tool_name(
    required_arg: Annotated[str, Field(description="Required argument as in functions")],
    optional_arg: Annotated[str, Field(description="Optional argument as in functions")] = "default value",
) -> str:
    """Tool description is here"""
    
    # ... your logic here ...
    
    return "result"

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

FastMCP("scaffold") names the server. @mcp.tool() turns the annotated function into a tool, and its docstring serves as the description. The function arguments need to be annoated with data type and description so that the server input schema could be inferred. The server explains itself via these descriptions.

To run the server, you need to install the SDK with pip install "mcp[cli]" on your device. mcp.run() then serves on the internet.

Connection

The server must be connected before any prompt attends to use it.

Below exemplifies how to connect to MCP server on VSCode. The reference can be found here.

.mcp.json
json
{
  "mcpServers": {
    "<server_name>": {
      "type": "stdio",
      "command": "python",
      "args": ["mcp_server.py"]
    }
  }
}

.mcp.json locates at the workspace root. <server_name> is the name that the client shows for this server. command and args are how it launches the server. type for stdio means the interaction uses standard input and output in console.

Most GUI clients have settings for connecting to MCP servers. For example, in VSCode, you may learn from this page to set up MCP servers with GUI operations.

Code example

Type a request in natural language and inpsect the internal process.

units_server.py
python
"""Convert temperatures and distances, as an MCP server."""

from typing import Annotated, Literal
from pydantic import Field
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("units")

@mcp.tool()
def convert_temperature(
    value: Annotated[float, Field(description="The temperature to convert")],
    to: Annotated[Literal["celsius", "fahrenheit"], Field(description="Unit to convert into")],
) -> str:
    """Convert a temperature between Celsius and Fahrenheit."""
    if to == "fahrenheit":
        result = value * 9 / 5 + 32
    else:
        result = (value - 32) * 5 / 9
    return f"{result:.1f}"

@mcp.tool()
def convert_distance(
    value: Annotated[float, Field(description="The distance to convert")],
    to: Annotated[Literal["km", "miles"], Field(description="Unit to convert into")],
) -> str:
    """Convert a distance between kilometres and miles."""
    if to == "km":
        result = value * 1.609344
    else:
        result = value / 1.609344
    return f"{result:.2f}"

if __name__ == "__main__":
    mcp.run()
You prompt in natural language
User
Convert 100F to C
How many km is a 10 mile run?
What is 0 degrees Celsius in Fahrenheit?
The client acquires the available tools from the server
Client
The model picks a tool and writes the arguments
Model
The server runs the tool
Server
The generated answer comes back to you
Model