MCP架构设计和实践应用
2025-11-23 17:05:40 0 举报
AI智能生成
MCP Host设计结合客户端Client和服务端Server的架构介绍,实现在大模型 LLM时代的智能应用。
作者其他创作
大纲/内容
什么是mcp
MCP 是一个开源的标准,用于连接AI应用和其他外部应用
概述mcp的几个主要项目
MCP Specification: A specification of MCP that outlines the implementation requirements for clients and servers.·
MCP SDKs: SDKs for different programming languages that implement MCP.
MCP Development Tools: Tools for developing MCP servers and clients, including the MCP Inspector
MCP Reference Server Implementations: Reference implementations of MCP servers.
概述mcp的主要构成
MCP Host: The AI application that coordinates and manages one or multiple MCP clients
案例
MCP Client: A component that maintains a connection to an MCP server and obtains context from an MCP server for the MCP host to use
基于uv构建客户端环境
初始构建客户端
连接服务端
MCP Server: A program that provides context to MCP clients
有什么特点
对于开发者能减少构建部署的复杂度,
对于AI应用来说能方便获得更多数据源,工具
用户端能更加获得更加方便,能力更全的应用,在你的交互场景中
mcp 的层级结构 MCP consists of two layers
Data layer:
Defines the JSON-RPC based protocol for client-server communication, including lifecycle management, and core primitives, such as tools, resources, prompts and notifications.
Transport layer:
Defines the communication mechanisms and channels that enable data exchange between clients and servers, including transport-specific connection establishment, message framing, and authorization.
mcp交互方式
The transport layer manages communication channels and authentication between clients and servers.
It handles connection establishment, message framing, and secure communication between MCP participants.
基于stdio的交互
交互时注意 控制台输出事项
本地的,高效的
基于http的交互
双边的,高效的,推荐的
流式交互,结构化的
基于sse的交互
单边的
单边流,不完全结构化的
基于FastMCP的服务进阶
mcp服务端
MCP服务的主要模块
资源: File-like data that can be read by clients (like API responses or file contents)
工具: Functions that can be called by the LLM (with user approval)
提示词模板: Pre-written templates that help users accomplish specific tasks
构建MCP服务端
定义常量和辅助函数
构建工具tool,使用装饰实现
构建资源resource
构建(参数)提示词prompt
mcp的传输交互
The client automatically infers the appropriate transport based on the input:
FastMCP instance → In-memory transport (perfect for testing)
File path ending in .py → Python Stdio transport
File path ending in .js → Node.js Stdio transport
URL starting with http:// or https:// → HTTP transport
MCPConfig dictionary → Multi-server client
FastMCP instance → In-memory transport (perfect for testing)
File path ending in .py → Python Stdio transport
File path ending in .js → Node.js Stdio transport
URL starting with http:// or https:// → HTTP transport
MCPConfig dictionary → Multi-server client
通常可以再开发环境使用In Memory的方式交互服务端,方便调试,减少网络层面的问题和复杂度。
mcp客户端
构建MCP客户端
连接单个服务端
多客户端配置方案
回调函数使用
mcp服务部署
mcp网络服务部署
The direct HTTP
The direct HTTP server approach is simpler and perfect for getting started quickly. You modify your server’s run() method to use HTTP transport, and FastMCP handles all the web server configuration. This approach works well for standalone deployments where you want your MCP server to be the only service running on a port.
The ASGI application
The ASGI application approach gives you more control and flexibility. Instead of running the server directly, you create an ASGI application that can be served by Uvicorn. This approach is better when you need advanced server features like multiple workers, custom middleware, or when you’re integrating with existing web applications.
0 条评论
下一页