Skip to content

Commit 3d7693a

Browse files
committed
update:优化文档
1 parent 5208b35 commit 3d7693a

3 files changed

Lines changed: 101 additions & 115 deletions

File tree

README.md

Lines changed: 3 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -19,122 +19,11 @@
1919
└─── 消息转发 ───────┴─── 消息转发 ───────┘
2020
```
2121

22-
## 安装依赖
22+
## 运行
2323

2424
```bash
25-
conda remove -n mcp-endpoint-server --all -y
26-
conda create -n mcp-endpoint-server python=3.10 -y
27-
conda activate mcp-endpoint-server
2825

29-
pip install -r requirements.txt
30-
31-
cp mcp-endpoint-server.cfg data/.mcp-endpoint-server.cfg
32-
33-
```
34-
35-
## 配置说明
36-
37-
### 配置文件位置
38-
- 示例配置: `mcp-endpoint-server.cfg`
39-
- 实际配置: `data/.mcp-endpoint-server.cfg`
40-
41-
### 配置项说明
42-
43-
```ini
44-
[server]
45-
host = 127.0.0.1 # 服务器监听地址
46-
port = 8004 # 服务器监听端口
47-
debug = false # 调试模式
48-
log_level = INFO # 日志级别
49-
50-
[websocket]
51-
max_connections = 1000 # 最大连接数
52-
ping_interval = 30 # 心跳间隔(秒)
53-
ping_timeout = 10 # 心跳超时(秒)
54-
close_timeout = 10 # 关闭超时(秒)
55-
56-
[security]
57-
allowed_origins = * # 允许的源
58-
enable_cors = true # 启用CORS
59-
60-
[logging]
61-
log_file = logs/mcp_server.log # 日志文件路径
62-
max_file_size = 10MB # 日志文件最大大小
63-
backup_count = 5 # 日志备份数量
64-
```
65-
66-
## 启动服务器
67-
68-
```bash
69-
python main.py
7026
```
7127

72-
### 测试数据
73-
```json
74-
{"id":0,"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"sampling":{},"roots":{"listChanged":false}},"clientInfo":{"name":"xz-mcp-broker","version":"0.0.1"}}}
75-
76-
77-
{"jsonrpc":"2.0","method":"notifications/initialized"}
78-
79-
80-
{"id":1,"jsonrpc":"2.0","method":"tools/list","params":{}}
81-
82-
83-
{"id":2,"jsonrpc":"2.0","method":"ping","params":{}}
84-
85-
86-
{"id":10,"jsonrpc":"2.0","method":"tools/call","params":{"name":"calculator","arguments":{"python_expression":"130000 * 130000"},"serialNumber":null}}
87-
```
88-
89-
### Docker部署
90-
91-
```dockerfile
92-
FROM python:3.9-slim
93-
94-
WORKDIR /app
95-
COPY requirements.txt .
96-
RUN pip install -r requirements.txt
97-
98-
COPY . .
99-
EXPOSE 8004
100-
101-
CMD ["python", "main.py"]
102-
```
103-
104-
## 监控和维护
105-
106-
### 日志查看
107-
```bash
108-
tail -f logs/mcp_server.log
109-
```
110-
111-
### 连接状态检查
112-
```bash
113-
curl http://127.0.0.1:8004/stats
114-
```
115-
116-
### 健康检查
117-
```bash
118-
curl http://127.0.0.1:8004/health
119-
```
120-
121-
## 开发说明
122-
123-
### 项目结构
124-
```
125-
mcp-endpoint-server/
126-
├── src/
127-
│ ├── core/ # 核心模块
128-
│ │ └── connection_manager.py
129-
│ ├── handlers/ # 处理器
130-
│ │ └── websocket_handler.py
131-
│ ├── utils/ # 工具模块
132-
│ │ ├── config.py
133-
│ │ └── logger.py
134-
│ └── server.py # 主服务器
135-
├── data/ # 配置文件目录
136-
├── logs/ # 日志目录
137-
├── main.py # 主入口
138-
├── requirements.txt # 依赖文件
139-
└── README.md # 说明文档
140-
```
28+
## 技术细节
29+
[技术细节](./README_dev.md)

README_dev.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# MCP Endpoint Server
2+
3+
## 安装依赖
4+
5+
```bash
6+
conda remove -n mcp-endpoint-server --all -y
7+
conda create -n mcp-endpoint-server python=3.10 -y
8+
conda activate mcp-endpoint-server
9+
10+
pip install -r requirements.txt
11+
12+
python main.py
13+
```
14+
15+
### 测试数据
16+
```json
17+
{"id":0,"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{"sampling":{},"roots":{"listChanged":false}},"clientInfo":{"name":"xz-mcp-broker","version":"0.0.1"}}}
18+
19+
20+
{"jsonrpc":"2.0","method":"notifications/initialized"}
21+
22+
23+
{"id":1,"jsonrpc":"2.0","method":"tools/list","params":{}}
24+
25+
26+
{"id":2,"jsonrpc":"2.0","method":"ping","params":{}}
27+
28+
29+
{"id":10,"jsonrpc":"2.0","method":"tools/call","params":{"name":"calculator","arguments":{"python_expression":"130000 * 130000"},"serialNumber":null}}
30+
```
31+
32+
## 开发说明
33+
34+
### 项目结构
35+
```
36+
mcp-endpoint-server/
37+
├── src/
38+
│ ├── core/ # 核心模块
39+
│ │ └── connection_manager.py
40+
│ ├── handlers/ # 处理器
41+
│ │ └── websocket_handler.py
42+
│ ├── utils/ # 工具模块
43+
│ │ ├── config.py
44+
│ │ └── logger.py
45+
│ └── server.py # 主服务器
46+
├── data/ # 配置文件目录
47+
├── logs/ # 日志目录
48+
├── main.py # 主入口
49+
├── requirements.txt # 依赖文件
50+
└── README.md # 说明文档
51+
```

src/utils/config.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os
66
import configparser
7+
import uuid
78
from typing import Optional
89
from pathlib import Path
910

@@ -20,17 +21,62 @@ def _load_config(self):
2021
"""加载配置文件"""
2122
if os.path.exists(self.config_file):
2223
self.config.read(self.config_file, encoding="utf-8")
24+
# 检查并生成key
25+
self._check_and_generate_key()
2326
else:
24-
# 如果配置文件不存在,使用默认配置
27+
# 如果配置文件不存在,从根目录拷贝
28+
self._copy_config_from_root()
29+
30+
def _copy_config_from_root(self):
31+
"""从根目录拷贝配置文件到data目录"""
32+
root_config = "mcp-endpoint-server.cfg"
33+
if os.path.exists(root_config):
34+
# 确保data目录存在
35+
os.makedirs(os.path.dirname(self.config_file), exist_ok=True)
36+
37+
# 拷贝配置文件
38+
import shutil
39+
40+
shutil.copy2(root_config, self.config_file)
41+
42+
# 重新加载配置
43+
self.config.read(self.config_file, encoding="utf-8")
44+
# 检查并生成key
45+
self._check_and_generate_key()
46+
else:
47+
# 如果根目录也没有配置文件,则创建默认配置
2548
self._create_default_config()
2649

50+
def _check_and_generate_key(self):
51+
"""检查key是否存在且长度足够,如果不足则生成新的"""
52+
try:
53+
current_key = self.config.get("server", "key", fallback="")
54+
if not current_key or len(current_key) < 32:
55+
# 生成32位随机密码
56+
new_key = self._generate_random_key()
57+
self.config.set("server", "key", new_key)
58+
59+
# 保存到配置文件
60+
with open(self.config_file, "w", encoding="utf-8") as f:
61+
self.config.write(f)
62+
63+
print(f"已自动生成新的32位密钥: {new_key}")
64+
except Exception as e:
65+
print(f"检查密钥时发生错误: {e}")
66+
67+
def _generate_random_key(self) -> str:
68+
"""生成指定长度的随机密钥"""
69+
# 使用UUID生成密钥,移除连字符
70+
return str(uuid.uuid4()).replace("-", "")
71+
2772
def _create_default_config(self):
2873
"""创建默认配置"""
2974
self.config["server"] = {
3075
"host": "127.0.0.1",
3176
"port": "8004",
3277
"debug": "false",
3378
"log_level": "INFO",
79+
"key": self._generate_random_key(), # 生成默认密钥
3480
}
3581

3682
self.config["websocket"] = {

0 commit comments

Comments
 (0)