烂翻译系列之——MCP中的概念——服务端
MCP servers are programs that expose specific capabilities to AI applications through standardized protocol interfaces.
MCP 服务端是通过标准化协议接口向 AI 应用公开特定功能的程序。
Common examples include file system servers for document access, database servers for data queries, GitHub servers for code management, Slack servers for team communication, and calendar servers for scheduling.
常见的示例包括:用于文档访问的文件系统服务端、用于数据查询的数据库服务端、用于代码管理的 GitHub 服务端、用于团队沟通的 Slack 服务端,以及用于日程安排的日程服务端。
Core Server Features 核心服务端功能
Servers provide functionality through three building blocks:
服务端通过以下三个基本构件提供功能:
Feature 功能特性 |
Explanation 说明 |
Examples 示例 |
Who controls it 控制方 |
---|---|---|---|
Tools 工具 |
Functions that your LLM can actively call, and decides when to use them based on user requests. Tools can write to databases, call external APIs, modify files, or trigger other logic. 大语言模型(LLM)可根据用户请求主动调用的功能,并自主决定何时使用。工具可执行写入数据库、调用外部 API、修改文件或触发其他逻辑等操作。 |
Search flights 搜索航班 |
Model |
Resources 资源 |
Passive data sources that provide read-only access to information for context, such as file contents, database schemas, or API documentation. 被动的数据源,为模型提供信息的只读访问权限,用于上下文补充,例如文件内容、数据库结构或 API 文档。 |
Retrieve documents 检索文档 Access knowledge bases 访问知识库 Read calendars 读取日程 |
Application |
Prompts 提示词 |
Pre-built instruction templates that tell the model to work with specific tools and resources. 预先构建的指令模板,指导模型如何使用特定的工具和资源来完成任务。 |
Plan a vacation 策划一次假期 Summarize my meetings 总结我的会议记录 Draft an email 草拟一封邮件 |
User |
We will use a hypothetical scenario to demonstrate the role of each of these features, and show how they can work together.
Tools 工具
Tools enable AI models to perform actions. Each tool defines a specific operation with typed inputs and outputs. The model requests tool execution based on context.
How Tools Work 工具的工作方式
Tools are schema-defined interfaces that LLMs can invoke. MCP uses JSON Schema for validation. Each tool performs a single operation with clearly defined inputs and outputs. Tools may require user consent prior to execution, helping to ensure users maintain control over actions taken by a model.
工具是大语言模型(LLM)可以调用的、由模式(schema)定义的接口。MCP 使用 JSON Schema 进行参数校验。每个工具执行单一操作,其输入和输出均有清晰定义。在执行前,工具可能需要获得用户的同意,从而确保用户对模型所采取的操作保持控制。
Protocol operations: 协议操作:
Method 方法 |
Purpose 目的 |
Returns 返回值 |
---|---|---|
tools/list |
Discover available tools 发现可用的工具 |
Array of tool definitions with schemas 包含模式定义的工具定义数组 |
tools/call |
Execute a specific tool 执行特定工具 |
Tool execution result 工具执行结果 |
Example tool definition: 工具定义示例:
{
name: "searchFlights",
description: "Search for available flights",
inputSchema: {
type: "object",
properties: {
origin: { type: "string", description: "Departure city" },
destination: { type: "string", description: "Arrival city" },
date: { type: "string", format: "date", description: "Travel date" }
},
required: ["origin", "destination", "date"]
}
}
Example: Travel Booking 示例:旅行预订
Tools enable AI applications to perform actions on behalf of users. In a travel planning scenario, the AI application might use several tools to help book a vacation:
工具使 AI 应用能够代表用户执行操作。在旅行规划的场景中,AI 应用可能会使用多个工具来协助预订度假:
Flight Search 航班搜索
searchFlights(origin: "NYC", destination: "Barcelona", date: "2024-06-15")
Queries multiple airlines and returns structured flight options.
查询多家航空公司,并返回结构化的航班选项。
Calendar Blocking 日程标记
createCalendarEvent(title: "Barcelona Trip", startDate: "2024-06-15", endDate: "2024-06-22")
Marks the travel dates in the user’s calendar.
在用户日程中标记旅行日期。
Email notification 邮件通知
sendEmail(to: "team@work.com", subject: "Out of Office", body: "...")
Sends an automated out-of-office message to colleagues.
自动向同事发送休假通知邮件。
User Interaction Model 用户交互模型
Tools are model-controlled, meaning AI models can discover and invoke them automatically. However, MCP emphasizes human oversight through several mechanisms.
工具由模型控制,这意味着 AI 模型可以自动发现并调用它们。然而,MCP 通过多种机制强调人类的监督与控制。
For trust and safety, applications can implement user control through various mechanisms, such as:
为了确保信任与安全,应用程序可通过以下机制实现用户控制:
- Displaying available tools in the UI, enabling users to define whether a tool should be made available in specific interactions 在用户界面(UI)中展示可用工具,允许用户决定在特定交互中是否启用某个工具;
- Approval dialogs for individual tool executions 在每次工具执行前弹出确认对话框,要求用户批准;
- Permission settings for pre-approving certain safe operations 设置权限选项,预先批准某些安全的操作;
- Activity logs that show all tool executions with their results 提供活动日志,记录所有工具的执行过程及其结果。
Resources 资源
Resources provide structured access to information that the AI application can retrieve and provide to models as context.
How Resources Work 资源的工作方式
Resources expose data from files, APIs, databases, or any other source that an AI needs to understand context. Applications can access this information directly and decide how to use it - whether that’s selecting relevant portions, searching with embeddings, or passing it all to the model.
资源公开来自文件、API、数据库或任何其他数据源的信息,帮助 AI 理解上下文。应用程序可直接访问这些信息,并自主决定如何使用——例如选择相关内容、通过嵌入向量进行搜索,或将全部数据传递给模型。
Each resource has a unique URI (like file:///path/to/document.md
) and declares its MIME type for appropriate content handling. They declare MIME types for appropriate content handling and support two discovery patterns:
每个资源都有一个唯一的 URI(例如 file:///path/to/document.md
),并声明其 MIME 类型,以便进行适当的内容处理。资源支持两种发现模式:
- Direct Resources - fixed URIs that point to specific data. Example:
calendar://events/2024
- returns calendar availability for 2024 直接资源(Direct Resources):指向特定数据的固定 URI。示例:calendar://events/2024
—— 返回 2024 年的日程安排信息。 - Resource Templates - dynamic URIs with parameters for flexible queries. Example: 资源模板(Resource Templates):包含参数的动态 URI,支持灵活查询。示例:
travel://activities/{city}/{category}
- returns activities by city and category 按城市和类别返回活动信息travel://activities/barcelona/museums
- returns all museums in Barcelona 返回巴塞罗那的所有博物馆信息
Resource Templates include metadata such as title, description, and expected MIME type, making them discoverable and self-documenting.
资源模板包含标题、描述和预期 MIME 类型等元数据,使其可被发现且具备自描述性。
Protocol operations: 协议操作:
Method 方法 |
Purpose 目的 |
Returns 返回值 |
---|---|---|
resources/list |
List available direct resources 列出可用的直接资源 |
Array of resource descriptors 资源描述符数组 |
resources/templates/list |
Discover resource templates 发现所有的资源模板 |
Array of resource template definitions 资源模板定义数组 |
resources/read |
Retrieve resource contents 读取资源内容 |
Resource data with metadata 包含元数据的资源数据 |
resources/subscribe |
Monitor resource changes 监听资源变更 |
Subscription confirmation 订阅确认信息 |
Example: Getting Travel Planning Context 示例:获取旅行规划上下文
Continuing with the travel planning example, resources provide the AI application with access to relevant information:
继续以旅行规划为例,资源为 AI 应用提供对相关信息的访问:
- Calendar data (
calendar://events/2024
) - Checks user availability 日程数据 (calendar://events/2024
) —— 检查用户的日程安排是否空闲 - Travel documents (
file:///Documents/Travel/passport.pdf
) - Accesses important documents 旅行文档 (file:///Documents/Travel/passport.pdf
) —— 访问重要文件,如护照信息 - Previous itineraries (
trips://history/barcelona-2023
) - References past trips and preferences 以往行程记录 (trips://history/barcelona-2023
) —— 参考过去的旅行记录和用户偏好
The AI application retrieves these resources and decides how to process them, whether selecting a subset of data using embeddings or keyword search, or passing raw data directly to the model.
AI 应用检索这些资源后,决定如何处理它们——可以使用嵌入向量或关键词搜索选取部分数据,也可以将原始数据直接传递给模型。
In this case, it provides calendar data, weather information, and travel preferences to the model, enabling it to check availability, look up weather patterns, and reference past travel preferences.
在此场景中,应用将日程数据、天气信息和旅行偏好提供给模型,使其能够核查行程可用性、查询天气趋势并参考历史旅行偏好。
Resource Template Examples: 资源模板示例:
{
"uriTemplate": "weather://forecast/{city}/{date}",
"name": "weather-forecast",
"title": "Weather Forecast",
"description": "Get weather forecast for any city and date",
"mimeType": "application/json"
}
{
"uriTemplate": "travel://flights/{origin}/{destination}",
"name": "flight-search",
"title": "Flight Search",
"description": "Search available flights between cities",
"mimeType": "application/json"
}
These templates enable flexible queries. For weather data, users can access forecasts for any city/date combination. For flights, they can search routes between any two airports. When a user has input “NYC” as the origin
airport and begins to input “Bar” as the destination
airport, the system can suggest “Barcelona (BCN)” or “Barbados (BGI)”.
这些模板支持灵活的查询。对于天气数据,用户可以获取任意城市和日期组合的天气预报;对于航班信息,用户可以搜索任意两个机场之间的航线。当用户输入“NYC”作为出发机场,并开始输入“Bar”作为目的地机场时,系统可以自动建议“巴塞罗那 (BCN)”或“巴巴多斯 (BGI)”等匹配选项。
Parameter Completion 参数补全
Dynamic resources support parameter completion. For example:
动态资源支持参数自动补全。例如:
- Typing “Par” as input for
weather://forecast/{city}
might suggest “Paris” or “Park City” 在输入weather://forecast/{city}
时,键入 “Par” 可能会提示 “Paris”(巴黎)或 “Park City”(帕克城); - Typing “JFK” for
flights://search/{airport}
might suggest “JFK - John F. Kennedy International” 在输入flights://search/{airport}
时,键入 “JFK” 可能会提示 “JFK - John F. Kennedy International”(约翰·肯尼迪国际机场)。
The system helps discover valid values without requiring exact format knowledge.
该系统有助于用户发现有效的参数值,而无需了解精确的格式要求。
User Interaction Model 用户交互模型
Resources are application-driven, giving them flexibility in how they retrieve, process, and present available context. Common interaction patterns include:
资源由应用程序驱动,这使得应用在检索、处理和呈现上下文信息方面具有高度灵活性。常见的交互模式包括:
- Tree or list views for browsing resources in familiar folder-like structures 以树状或列表视图浏览资源,呈现类似文件夹的熟悉结构;
- Search and filter interfaces for finding specific resources 提供搜索和筛选界面,便于查找特定资源;
- Automatic context inclusion or smart suggestions based on heuristics or AI selection 基于启发式规则或 AI 判断,自动包含上下文或提供智能建议;
- Manual or bulk selection interfaces for including single or multiple resources 支持手动选择或批量选择,用于添加单个或多个资源。
Applications are free to implement resource discovery through any interface pattern that suits their needs. The protocol doesn’t mandate specific UI patterns, allowing for resource pickers with preview capabilities, smart suggestions based on current conversation context, bulk selection for including multiple resources, or integration with existing file browsers and data explorers.
应用程序可自由选择适合其需求的接口模式来实现资源发现。该协议不限定具体的UI形式,因此可以实现带预览功能的资源选择器、基于当前对话上下文的智能推荐、批量选择多个资源,或与现有的文件浏览器和数据浏览器无缝集成。
Prompts 提示词
Prompts provide reusable templates. They allow MCP server authors to provide parameterized prompts for a domain, or showcase how to best use the MCP server.
How Prompts Work 提示词的工作方式
Prompts are structured templates that define expected inputs and interaction patterns. They are user-controlled, requiring explicit invocation rather than automatic triggering. Prompts can be context-aware, referencing available resources and tools to create comprehensive workflows. Similar to resources, prompts support parameter completion to help users discover valid argument values.
提示词是结构化的模板,用于定义预期的输入和交互模式。它们由用户控制,必须由用户显式调用,不会被自动触发。提示词可以具备上下文感知能力,能够引用当前可用的资源和工具,从而构建完整的任务工作流。与资源类似,提示词也支持参数补全,帮助用户发现有效的参数值。
Protocol operations: 协议操作:
Method 方法 |
Purpose 提示词 |
Returns 返回值 |
---|---|---|
prompts/list |
Discover available prompts 发现可用的提示词 |
Array of prompt descriptors 提示词描述符数组 |
prompts/get |
Retrieve prompt details 获取提示词详细信息 |
Full prompt definition with arguments 包含参数的完整提示词定义 |
Example: Streamlined Workflows 示例:简化工作流程
Prompts provide structured templates for common tasks. In the travel planning context:
提示词为常见任务提供结构化模板。在旅行规划场景中:
“Plan a vacation” prompt: “策划一次度假”提示词:
{
"name": "plan-vacation",
"title": "Plan a vacation",
"description": "Guide through vacation planning process",
"arguments": [
{ "name": "destination", "type": "string", "required": true },
{ "name": "duration", "type": "number", "description": "days" },
{ "name": "budget", "type": "number", "required": false },
{ "name": "interests", "type": "array", "items": { "type": "string" } }
]
}
Rather than unstructured natural language input, the prompt system enables:
与非结构化的自然语言输入不同,提示词系统支持:
- Selection of the “Plan a vacation” template 选择“策划一次度假”模板
- Structured input: Barcelona, 7 days, $3000, [“beaches”, “architecture”, “food”] 结构化输入:巴塞罗那、7天、3000美元、["海滩", "建筑", "美食"]
- Consistent workflow execution based on the template 基于模板的标准化工作流程执行
User Interaction Model 用户交互模型
Prompts are user-controlled, requiring explicit invocation. The protocol gives implementers freedom to design interfaces that feel natural within their application. Key principles include:
提示词由用户控制,必须通过显式操作来调用。该协议允许实现者自由设计符合其应用风格的用户界面。关键设计原则包括:
- Easy discovery of available prompts 便于发现可用的提示词
- Clear descriptions of what each prompt does 清晰描述每个提示词的功能
- Natural argument input with validation 自然的参数输入体验,并包含输入验证
- Transparent display of the prompt’s underlying template 透明展示提示词背后的模板内容
Applications typically expose prompts through various UI patterns such as:
应用程序通常通过以下多种UI模式来提供提示词:
- Slash commands (typing ”/” to see available prompts like /plan-vacation) 斜杠命令(输入“/”后显示可用提示词,如
/plan-vacation
) - Command palettes for searchable access 命令面板,支持搜索和快速访问
- Dedicated UI buttons for frequently used prompts 专用 UI 按钮,用于高频使用的提示词
- Context menus that suggest relevant prompts 上下文菜单,根据当前场景推荐相关提示词
Bringing Servers Together 整合多个服务端
The real power of MCP emerges when multiple servers work together, combining their specialized capabilities through a unified interface.
Example: Multi-Server Travel Planning 示例:多服务端协同的旅行规划
Consider a personalized AI travel planner application, with three connected servers:
设想一个个性化的 AI 旅行规划应用,连接了以下三个服务端:
- Travel Server - Handles flights, hotels, and itineraries 旅行服务端:负责处理航班、酒店和行程安排
- Weather Server - Provides climate data and forecasts 天气服务端:提供气候数据和天气预报
- Calendar/Email Server - Manages schedules and communications 日程/邮件服务端:管理用户日程和通信
The Complete Flow 完整流程
-
User invokes a prompt with parameters: 用户调用一个带有参数的提示词:
{ "prompt": "plan-vacation", "arguments": { "destination": "Barcelona", "departure_date": "2024-06-15", "return_date": "2024-06-22", "budget": 3000, "travelers": 2 } }
- User selects resources to include: 用户选择要包含的资源:
calendar://my-calendar/June-2024
(from Calendar Server) (来自日程服务端)travel://preferences/europe
(from Travel Server) (来自旅行服务端)travel://past-trips/Spain-2023
(from Travel Server) (来自旅行服务端)
-
AI processes the request using tools: The AI first reads all selected resources to gather context - identifying available dates from the calendar, learning preferred airlines and hotel types from travel preferences, and discovering previously enjoyed locations from past trips. Using this context, the AI then executes a series of Tools:
AI 使用工具处理请求:AI 首先读取所有选定的资源以获取上下文信息——从日程中识别可用日期,从旅行偏好中了解用户偏好的航空公司和酒店类型,并从过往行程中发现用户曾喜欢的地点。基于这些上下文,AI 随后调用一系列工具:
searchFlights()
- Queries airlines for NYC to Barcelona flights 查询从纽约(NYC)到巴塞罗那的航班checkWeather()
- Retrieves climate forecasts for travel dates 获取旅行日期期间的天气预报
The AI then uses this information to create the booking and following steps, requesting approval from the user where necessary:
AI 接着利用这些信息制定预订方案,并在必要时请求用户批准以下操作:
bookHotel()
- Finds hotels within the specified budget 在指定预算内查找并预订酒店createCalendarEvent()
- Adds the trip to the user’s calendar 将行程添加到用户的日程中sendEmail()
- Sends confirmation with trip details 发送包含行程详情的确认邮件
The result: Through multiple MCP servers, the user researched and booked a Barcelona trip tailored to their schedule. The “Plan a Vacation” prompt guided the AI to combine Resources (calendar availability and travel history) with Tools (searching flights, booking hotels, updating calendars) across different servers—gathering context and executing the booking. A task that could’ve taken hours was completed in minutes using MCP.
结果:通过多个 MCP 服务端的协同工作,用户快速完成了一次量身定制的巴塞罗那之旅的调研与预订。借助“策划一次度假”这一提示词,AI 能够跨不同服务端整合资源(日程空闲时间和旅行历史)与工具(搜索航班、预订酒店、更新日程),完成上下文收集并执行预订操作。原本可能耗时数小时的任务,在 MCP 的支持下几分钟内即可完成。
