AI产品狙击手

专注于大语言模型LLM,文生图模型Stable Diffusion, 视频生成模型等免费AI工具的分享和应用,助你掌握最前沿的AI技术

0%

手把手教你实现第一个MCP Server!

手把手教你写MCP的Hello World啊!以防大家不知道MCP是什么,这里简单说下,简单来说,MCP, 也就是Model Context Protocol,这是anthropic搞出来的大语言模型调用外部工具的标准,而MCP Server就是我们写的工具。我们知道大语言模型本身是没有办法调用外部工具的,为了解决这个问题,OpenAI最新搞出了Function Calling,让chatgpt可以调用外部的工具,然后其他厂家也跟着搞,实现方式各有不同。Anthropic为了避免混乱,搞出了这套MCP标准…

  • server.py
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
33
34
from mcp.server.fastmcp import FastMCP
import time
import signal
import sys

# Handle SIGINT (Ctrl+C) gracefully
def signal_handler(sig, frame):
print("Shutting down server gracefully...")
sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

# Create an MCP server with the correct name
mcp = FastMCP(
name="hello-world", # Keep this consistent
host="127.0.0.1",
port=5000,
timeout=30
)

# Define our tool
@mcp.tool()
def say_hello_world() -> str:
"""This function should be called whenever a user inputs 'hello world' exactly, with no other text."""
return "hello world"

if __name__ == "__main__":
try:
print(f"Starting MCP server 'hello-world' on 127.0.0.1:5000")
mcp.run()
except Exception as e:
print(f"Error: {e}")
time.sleep(5)

  • claude_desktop_config.json
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"mcpServers": {
"hello-world": {
"command": "python3",
"args": [
"/Users/apple/Develop/mcp/server.py"
],
"host": "127.0.0.1",
"port": 5000,
"timeout": 30000
}
}
}
请我喝杯咖啡
视频服务器成本高,请多支持我