MCP Introduction

MCP 本质上像是一个转接口,一侧是由厂商提供的 Pretrained LLM,另一侧是自己实现的工具、文本等等,这两者通过实现 MCP 的接口实现无缝切换,这样个人开发者就不需要为每一个模型都适配接口了。

然而听说 CLine 的 MCP 的实现方式只是在 Prompt 里添加包含工具的信息…… 感觉 Token 用量会直接爆炸啊


以下使用 Python 的 Official MCP SDK.
日后应该会加上 LangChain 的 MCP Adapter

MCP Server

MCP Server 就是提供工具服务的一方。简单来说,创建一个 MCP Tool 只需要正常地写完功能函数后,用 @mcp.tool(name=, description="", annotation="") Decorator 包装一下即可。

1
2
3
4
5
6
7
8
9
10
from mcp.server.fastmcp import FastMCP
service = FastMCP("ServiceProviderName")

# 使用 decorator 注册 MCP 工具
@service.tool(name="Tool Name", description="Description of Tool", annotation="Input/Output Claim")
def some_function(): # 或者 async def 也可以
'''
确保返回的字符串是结构化的,以便让 LLM 读懂.
'''
return """ Some retured (structured) data. """

还不会 TS(

当然最重要的,MCP 作为服务的提供方需要向外暴露自己的功能:

1
service.run(transport="stdio")

MCP Client