Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MCP Getting Started Tutorial

MCP Getting Started Tutorial

MCP's full English name is Model Context Protocol (model context protocol), an open standard protocol that standardizes how AI applications connect to external tools and data sources.

MCP, just like USB-C, lets different devices connect together through the same interface.

Before MCP, every time a new tool (e.g. GitHub, Slack, a database) was added, each AI application had to write its own integration code, creating the N × M integration problem (N AI apps × M tools = N×M sets of code).

With MCP, tool providers only need to build one MCP Server per the standard, and any MCP-compatible AI application can use it directly, becoming 1 Server + compatible with all Hosts.

The figure below intuitively shows the difference between traditional N×M integration and MCP standardized integration:

Comparison diagram of traditional N×M integration vs MCP standardized integration

In December 2025, MCP was donated by Anthropic to the Agentic AI Foundation (AAIF) under the Linux Foundation, becoming an industry-shared open standard. Vendors such as OpenAI and Block jointly participate in its governance.

Development timeline

Time Milestone
Before 2023 OpenAI launched function calling and the ChatGPT plugin framework, but both were vendor-proprietary formats tied to a single platform
November 2024 Anthropic released MCP: model-agnostic, vendor-neutral, any model can use the same Server
March 2025 OpenAI officially announced support for MCP
Mid 2025 Added capabilities such as Streamable HTTP transport, OAuth 2.1 authorization, and Elicitation (information solicitation); MCP moved toward production-grade remote services
December 2025 Protocol governance transferred to AAIF (Linux Foundation), entering a multi-vendor co-governance phase
2026 The core evolves toward statelessness, adding extension capabilities such as Tasks (long-running tasks) and MCP Apps (interactive UI)

Core architecture: Host / Client / Server

MCP borrows the design idea of LSP (Language Server Protocol, the protocol by which editors uniformly support programming languages) and defines three roles:

Role Description Example
Host The AI application the user directly uses, responsible for coordinating multiple Clients and managing conversations Claude Code, Claude Desktop, Cursor, ChatGPT
Client An internal component of the Host, maintaining a one-to-one connection with a Server and responsible for translating the model's tool-call requests into standard protocol messages The Host creates one Client for each Server it connects to
Server Wraps a layer of standard protocol, exposing the capabilities of a specific tool or data source, and does not talk to the large model directly GitHub API Server, database Server, filesystem Server

The three-layer architecture relationship is shown in the figure below:

MCP three-tier architecture diagram: Host / Client / Server

The underlying communication uniformly uses the JSON-RPC 2.0 message format, ensuring standardized interaction between different roles.


Detailed explanation of the MCP communication flow

Take "a user asking the AI to check the weather" as an example; one complete tool-call process is as follows:

MCP communication flow sequence diagram: the complete chain from a user question to obtaining results

The flow has seven steps:

  1. The user asks: the user enters "help me check the weather in Beijing" in the Host (e.g. Claude Code).
  2. The model decides: the Host's built-in large model analyzes the problem, decides it needs to call a weather-query tool, and sends the call intent to the Client.
  3. Protocol translation: the Client converts the tool-call request into a JSON-RPC 2.0 format message and sends it to the corresponding Server.
  4. Server execution: after receiving the request, the Server performs the actual operation (e.g. calling a weather API, querying a database).
  5. Return result: the Server returns the result to the Client in JSON-RPC format.
  6. Back to the model: the Client passes the result back to the Host for the model to integrate into its answer.
  7. Answer the user: the model integrates the data returned by the tool with natural language and outputs the final answer.

Throughout the whole flow, the Server is completely unaware of the large model's existence: it only handles JSON-RPC messages, which is the key design that makes MCP model-agnostic.


Three core primitives

An MCP Server can expose three kinds of capabilities, called "primitives" (Primitive):

Primitive define Typical scenario
Tools Executable actions that the model can actively call and that produce side effects (e.g. writing data, sending messages) Sending email, creating tickets, executing database queries, manipulating files
Resources Read-only data for the model to read as context, with no execution involved Log file contents, database table snapshots, API documentation
Prompts (prompt templates) Pre-written reusable prompt templates that a user or application can call directly "Help me summarize the key clauses of this contract", "Analyze the performance issues of this code"

There are also two advanced mechanisms:

At the beginning you can focus on just Tools and Resources; Prompts, Sampling, and Elicitation can be learned later when you use them.


Transport methods

MCP supports two transport methods for different scenarios:

Transport methods Applicable scenarios Feature
stdio (standard input/output) Local tools: the Host starts the Server as a subprocess Simplest, requires no network configuration, suitable for local tools on a personal computer
Streamable HTTP Remote production environments, where the Server is deployed as an HTTPS service Supports concurrent connections and horizontal scaling, with OAuth 2.1 for authentication

For personal learning and local development, stdio is enough.

Enterprise scenarios that need multi-user sharing use Streamable HTTP.

All demo cases in this tutorial use stdio.


Security notes

MCP gives AI the ability to execute code and access real systems, so security practices are especially important:


Hands-on demo: connecting an MCP Server to Claude Code

Below, two complete cases show how to write an MCP Server and connect it to Claude Code.

The first case uses Python to write a weather-query service, the second uses Node.js/TypeScript to write to-do management.

Case 1: Python weather-query Server

Goal: write an MCP Server that provides a weather-query tool and call it through Claude Code.

Environment Preparation



pip install mcp httpx

Two Python packages need to be installed: mcp is the official MCP Python SDK, httpx is used to make HTTP requests to call the weather API.

Write the Server code

Create the file weather_server.py:

Example

File path: weather_server.py

from

mcp.

server

.

fastmcp

import

FastMCP

import

httpx

Create an MCP Server instance named "weather"

FastMCP provides a high-level API that simplifies Server development

mcp

=

FastMCP

(

"weather"

)

@

mcp.

tool

(

)

async

def

get_weather

(

city:

str

)

str

:

   

"""Query the current weather of the specified city.

    Args:
        city: city name, supports Chinese or English, e.g. "Beijing" or "Shanghai"
    """

   

Uses the free wttr.in weather API, no API Key registration required

   

The format parameter specifies the return format: %C=weather condition, %t=temperature

    url

=

f

"https://wttr.in/{city}?format=%C+%t"

    async

with

httpx.

AsyncClient

(

)

as

client:

        resp

=

await client.

get

(

url

,

timeout

=

10

)

        resp.

raise_for_status

(

)

       

return

f

"{city} current weather: {resp.text.strip()}"

@

mcp.

resource

(

"weather://cities/supported"

)

def

supported_cities

(

)

str

:

   

"""Returns the list of cities currently supported for query (read-only resource example)."""

   

return

"Beijing, Shanghai, Guangzhou, Shenzhen, Hangzhou, Chengdu, runoob"

if

name

==

"main"

:

   

Uses stdio transport, for Claude Code to call as a subprocess

    mcp.

run

(

transport

=

"stdio"

)

Code explanation:

Local debugging (optional but recommended)

Before connecting to Claude Code, first debug the Server with the official MCP Inspector tool to confirm it works correctly:



npx @modelcontextprotocol/inspector python weather_server.py

After running it, a web debugging interface opens automatically, where you can see which Tools and Resources the Server exposes and call them manually to test.

It is recommended to go through the Inspector debugging flow every time and fix code issues before connecting to a real AI assistant; this saves a lot of debugging time.

Configuring the MCP Server in Claude Code

Method 1: let the AI add it directly

We can just start Claude:

claude

Enter the following information and let Claude configure and add it for me:



添加 @weather_server.py 这个 MCP 服务

This is the simplest: just enter a prompt, and configuration and dependencies are all completed automatically.

You can see that configuration info is automatically generated in the .mcp.json file in the current directory:

After configuration, just ask directly in Claude Code:



帮我查一下北京现在的天气

If you like to tinker yourself, you can also use command-line addition and file configuration.

Method 2: command-line addition

Note that you need to modify the command and file paths yourself:



claude mcp add weather python /Users/runoob/runoob-test/weather_server.py

The meaning of this command:

After adding successfully, you can use the following command to view connected MCP Servers:



claude mcp list

Example output:



NAME      STATUS   COMMAND

weather   active   python /Users/runoob/runoob-test/weather_server.py

To remove a Server:



$ claude mcp remove weather

Method 3: configuration file (suitable for team sharing)

Create a .mcp.json file in the project root directory:



{

  "mcpServers": {

    "weather": {

      "command": "python",

      "args": ["/Users/runoob/runoob-test/weather_server.py"]

    }

  }

}

Save and restart Claude Code (or reopen the project), and the configuration takes effect.

The advantage of the configuration-file approach is that it can be committed to a Git repository, so team members automatically get the same MCP Server configuration after cloning the project.

If you configure a user-level MCP Server (stored in ~/.claude/mcp.json), it takes effect for all projects. A project-level .mcp.json only takes effect for the current project.

Actual usage

After configuration, just ask directly in Claude Code:



> 帮我查一下北京现在的天气

Claude Code will automatically:

  1. Recognize that the get_weather tool of the weather Server needs to be called.
  2. Pop up an authorization confirmation dialog asking whether to allow the call.
  3. After you click Agree, call the Server to get weather data.
  4. Integrate the result into a natural-language answer.

This is the complete loop from the model to real-world data.

Case 2: Node.js/TypeScript to-do Server

If you are more familiar with the JavaScript/TypeScript ecosystem, here is a Node.js version of the MCP Server example.

Environment Preparation



npm install @modelcontextprotocol/sdk zod

Write the Server code

Create the file todo_server.ts:

Example

// File path: todo_server.ts

import

{

McpServer

}

from

"@modelcontextprotocol/sdk/server/mcp.js"

;

import

{

StdioServerTransport

}

from

"@modelcontextprotocol/sdk/server/stdio.js"

;

import

{

z

}

from

"zod"

;

// Create an MCP Server instance, set the name and version number

const

server

=

new

McpServer

(

{

  name

:

"todo-list"

,

  version

:

"1.0.0"

}

)

;

// Use an array to simulate to-do storage (can be replaced by a database in real projects)

const

todos

:

string

[

]

=

[

]

;

// Register the first Tool: add a to-do item

server.

tool

(

 

"add_todo"

,

                       

// Tool name

 

"Add a to-do item"

,

                 

// Tool description (the model uses this to decide when to call it)

 

{

   

// Parameter schema, defined with Zod, automatically generates JSON Schema

    item

:

z.

string

(

)

.

describe

(

"To-do content"

)

 

}

,

  async

(

{

item

}

)

=>

{

    todos.

push

(

item

)

;

   

return

{

      content

:

[

{

        type

:

"text"

,

        text

:

`Added:

"${item}"

, current total $

{

todos.

length

}

to-do items`

     

}

]

   

}

;

 

}

)

;

// Register the second Tool: list all to-dos

server.

tool

(

 

"list_todos"

,

 

"List all to-do items"

,

 

{

}

,

                               

// No parameters

  async

(

)

=>

{

   

const

text

=

todos.

length

     

?

todos.

map

(

(

t

,

i

)

=>

`$

{

i

1

}

. $

{

t

}

`

)

.

join

(

"\n"

)

     

:

"No to-do items yet"

;

   

return

{

      content

:

[

{

type

:

"text"

,

text

}

]

   

}

;

 

}

)

;

// Start the Server using stdio transport

const

transport

=

new

StdioServerTransport

(

)

;

await server.

connect

(

transport

)

;

The way to connect to Claude Code is exactly the same as the Python version.

We can just start Claude:

claude

Enter the following information and let Claude configure and add it for me:



添加 @todo_server.ts 这个 MCP 服务

Then say "Add a to-do: buy the runoob tutorial" in the conversation, and Claude Code will call the add_todo tool.


Existing ecosystem: you don't have to write everything yourself

Since MCP was released, the community has published over 500 public Servers, covering the vast majority of common scenarios:

Category Ready-made MCP Server examples
Code hosting GitHub, GitLab, Bitbucket
Instant messaging Slack, Discord
Cloud storage Google Drive, OneDrive
Database PostgreSQL, MySQL, SQLite, MongoDB
Web automation Puppeteer, Playwright
Knowledge management Notion, Obsidian
Filesystem Filesystem (local file read/write)

Official SDKs cover the following programming languages: TypeScript, Python, C#, Java, Swift.

In real development, for most scenarios you can just find a ready-made Server to use. Only when connecting to internal company systems or private databases do you need to write your own MCP Server.


Common questions

Problem Answer
Will MCP replace Function Calling? No. Function Calling is the underlying mechanism by which models call tools; MCP wraps a standardized protocol around this mechanism so that models from different vendors all use the same tool definitions. The two are in a "standardization" relationship, not "replacement".
Can one Server be used by multiple AI applications at the same time? Yes, this is exactly MCP's core value: write a Server once, and any MCP-compatible Host (Claude Code, Claude Desktop, Cursor, ChatGPT, etc.) can use it.
Do you need to write code to use MCP? If you only use an MCP Server someone else built, no coding is needed at all: just configure it with the claude mcp add command in Claude Code. You only need to write code when developing your own Server.
How to choose between stdio and Streamable HTTP? For personal use and local tools, choose stdio (zero configuration); for team sharing and production deployment, choose Streamable HTTP (requires configuring HTTPS and OAuth).

References