MCP

Model Context Protocol.

简要介绍

model-context-protocol

mcp-intro


Model Context Protocol

模型 上下文 协议


用途

w:1000


MCP Host

  • 支持MCP协议的软件 比如:
    • Claude Code
    • Cursor
    • Cline
    • Gemini Cli

w:1000


MCP Server 和 Tool

  • MCP Server(MCP服务器)
    • 不是传统意义上的服务器
    • 本质就是一个程序,程序执行符合MCP协议,内置了一些功能模块解决对应问题
      • 功能模块 在MCP领域内叫:Tool(函数)
    • 大部分MCP Server都是在本地通过Python或Node启动

h:650


weather-mcp-server

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package tools

import (
    "github.com/mark3labs/mcp-go/mcp"
    "github.com/mark3labs/mcp-go/server"

    "github.com/ezh0v/weather-mcp-server/internal/server/handlers"
    "github.com/ezh0v/weather-mcp-server/internal/server/services"
)

func CurrentWeather(svc services.Services) (mcp.Tool, server.ToolHandlerFunc) {
    tool := mcp.NewTool("current_weather",
        mcp.WithDescription(`
            The service retrieves the current weather for a specified city and returns only HTML and CSS code designed 
            for displaying weather information directly on a canvas. No additional comments should be included after the response. 
            It processes the city's name, gathers weather data such as temperature, humidity, wind speed, and general weather conditions, 
            and then generates a structured HTML layout along with appropriate CSS styles. This HTML must be rendered visually on the canvas. 
            After receiving the response, the text inside the HTML should be translated into the language of the request.
        `),
        mcp.WithString("city",
            mcp.Required(),
            mcp.Description(`
                The name of the city. This field is required and must be provided in English. 
                Only one city is allowed, and it must be the last one provided by the user.
            `),
        ),
    )

    handler := handlers.CurrentWeather(svc)

    return tool, handler
}

MCP交互流程详解

Vicissitude🐳
Built with Hugo
主题 StackJimmy 设计