LM

liaokongvfx/mcp-chinese-getting-started-guide

Developer tools

This page was translated automatically. Technical details may differ from the original. View the original

3.6K stars 234 forks Quality 66 Trend 27

Model Context Protocol (MCP) - Programming Speedy Start

Overview

The Model Context Protocol (MCP) is an innovative open-source protocol that redefines how large language models (LLMs) interact with the outside world. MCP provides a standardized approach that allows any large language model to easily connect to various data sources and tools, enabling seamless access and processing of information. MCP acts like the USB-C interface for AI applications, providing AI models with a standardized way to connect different data sources and tools. - Resources - Prompts - Tools - Sampling - Roots - Transports Transport layer Since most functions actually serve the Claude client, this article prefers to write MCP server services and general large language models, so this article will mainly focus on "tools," with other features briefly explained at the end. Among them, MCP's transport layer supports two protocols: stdio (standard input/output) and SSE (server sends events). Since stdio is more commonly used, this article will use stdio as an example for explanation. This article will use the 3.11 version of Python and manage Python projects with UV. At the end of the article, the code will be posted on GitHub. Without further ado, let's get started~ In this section, we will implement a server for web search. First, we will initialize our project using UV. Next, let's create a file called web_search.py to implement our services. MCP provides us with two objects: mcp.server.FastMCP and mcp.server.Server. mcp.server.FastMCP is a higher-level encapsulation, and we'll use it here. The implementation is very simple. MCP provides us with a @mcp.tool(). We just need to decorate the implementation function with this decorator. The function name will be the tool name, the parameter will be the tool parameter, and the tool, parameters, and return values will be described with comments.

Overview (machine translated)

[TOC]

Introduction

The Model Context Protocol (MCP) is an innovative open-source protocol that redefines how large language models (LLMs) interact with the outside world. MCP provides a standardized approach that allows any large language model to easily connect to various data sources and tools, enabling seamless access and processing of information. MCP acts like the USB-C interface for AI applications, providing AI models with a standardized way to connect different data sources and tools.

MCP has the following core functions:

  • Resources
  • Prompts: Prompt words
  • Tools
  • Sampling sampling
  • Roots
  • Transports layer

Since most functions actually serve the Claude client, this article prefers to write MCP server services and general large language models, so this article will mainly focus on “tools,” with other features briefly explained at the end.

Among them, MCP’s transport layer supports two protocols: stdio (standard input/output) and SSE (server sends events). Since stdio is more commonly used, this article will use stdio as an example for explanation.

This article will use the 3.11 version of Python and manage Python projects with UV. At the end of the article, the code will be posted on GitHub. Without further ado, let’s get started~

Developing MCP Servers

In this section, we will implement a server for web search. First, we will initialize our project using UV.

UV official documentation: https://docs.astral.sh/uv/

Next, let’s create a file called web_search.py to implement our service. MCP provides us with two objects: mcp.server.FastMCP and mcp.server.Server. mcp.server.FastMCP is a higher-level package, which we will use here.

The implementation method is very simple. MCP provides us with a @mcp.tool() that we just need to decorate the implementation function with this decorator. The function name will be the tool name, the parameter will be the tool parameter, and the tool and parameters will be described with comments, as well as the return values.

Here, we directly use the Zhipu interface. This interface not only helps us search for related result links but also generates the summaries of articles from those links, , and currently it’s free (charging has already started, 0.03 yuan per entry), which is very suitable for us.

official documentation: https://bigmodel.cn/dev/api/search-tool/web-search-pro

API Key Generation Address: https://bigmodel.cn/usercenter/proj-mgmt/apikeys

Finally, let’s add the code to run the server.

Debugging the MCP Server

At this point, we complete the writing of the MCP server.

This page was translated automatically. Technical details may differ from the original.

Install

npx -y @modelcontextprotocol/inspector <command> <arg1> <arg2>
New to MCP? Start with the mcpskill.org guide →

Configuration

{ "mcpServers": { "web-search-server": { "command": "uv", "args": [ "--directory", "D:/projects/mcp_getting_started", "run", "web_search.py" ] } } }