Skip to content

Commit 582bdde

Browse files
committed
update project structure
1 parent 89f2207 commit 582bdde

File tree

19 files changed

+646
-236
lines changed

19 files changed

+646
-236
lines changed

README.md

Lines changed: 184 additions & 205 deletions
Large diffs are not rendered by default.

bin/mcp-serve

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env python3
2+
"""
3+
MCP Server - dynamically loads tools from config file
4+
5+
This is a standalone executable script for running MCP servers.
6+
It can be used by MCP clients to start servers with different configurations.
7+
8+
Usage:
9+
mcp-serve config.json
10+
mcp-serve config.json --mode streamable-http --port 8080
11+
"""
12+
13+
import argparse
14+
import sys
15+
from pathlib import Path
16+
17+
try:
18+
from mcpify.wrapper import MCPWrapper
19+
except ImportError:
20+
print("❌ Error: mcpify package not found. Please install it with:")
21+
print(" pip install mcpify")
22+
sys.exit(1)
23+
24+
25+
def main():
26+
"""Main entry point for the MCP server"""
27+
parser = argparse.ArgumentParser(
28+
description="MCP Server - loads tools from config file", prog="mcp-serve"
29+
)
30+
parser.add_argument("config_file", help="Path to the MCP configuration file")
31+
parser.add_argument(
32+
"--mode",
33+
choices=["stdio", "streamable-http"],
34+
default="stdio",
35+
help="Server mode (default: stdio)",
36+
)
37+
parser.add_argument(
38+
"--host", default="localhost", help="Host for HTTP mode (default: localhost)"
39+
)
40+
parser.add_argument(
41+
"--port", type=int, default=8080, help="Port for HTTP mode (default: 8080)"
42+
)
43+
44+
args = parser.parse_args()
45+
46+
# Check if config file exists
47+
config_file = Path(args.config_file)
48+
if not config_file.exists():
49+
print(f"❌ Error: Configuration file does not exist: {config_file}")
50+
sys.exit(1)
51+
52+
try:
53+
# Create wrapper
54+
wrapper = MCPWrapper(str(config_file))
55+
56+
# Start server based on mode
57+
if args.mode == "stdio":
58+
print("🚀 Starting MCP server in stdio mode...")
59+
wrapper.run()
60+
elif args.mode == "streamable-http":
61+
print(
62+
f"🚀 Starting MCP server in HTTP mode on " f"{args.host}:{args.port}..."
63+
)
64+
mcp_server = wrapper.server()
65+
66+
# Start backend if needed
67+
if wrapper.adapter:
68+
import asyncio
69+
70+
asyncio.run(wrapper.start_backend())
71+
72+
try:
73+
mcp_server.run(transport="sse", host=args.host, port=args.port)
74+
finally:
75+
if wrapper.adapter:
76+
import asyncio
77+
78+
asyncio.run(wrapper.stop_backend())
79+
80+
except KeyboardInterrupt:
81+
print("\n🛑 Server stopped.")
82+
except Exception as e:
83+
print(f"❌ Error starting server: {e}")
84+
sys.exit(1)
85+
86+
87+
if __name__ == "__main__":
88+
main()

docs/usage.md

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
# MCPify Usage Guide
2+
3+
MCPify 提供了简化的命令行界面,将"生成"和"运行"分离开来,让MCP客户端可以更灵活地控制服务器的启动。
4+
5+
## 项目架构
6+
7+
```
8+
mcpify/
9+
├── mcpify/ # 核心包
10+
│ ├── cli.py # CLI入口 (mcpify命令)
11+
│ ├── server.py # 服务器入口 (mcp-server命令)
12+
│ ├── wrapper.py # MCP包装器
13+
│ ├── backend.py # 后端适配器
14+
│ ├── detect.py # API检测
15+
│ └── validate.py # 配置验证
16+
├── bin/ # 独立可执行脚本
17+
│ └── mcp-serve # 独立服务器脚本
18+
├── examples/ # 示例配置文件
19+
│ ├── fastapi-example.json
20+
│ ├── python-module-example.json
21+
│ └── commandline-example.json
22+
└── docs/ # 文档
23+
└── usage.md
24+
```
25+
26+
## 命令概览
27+
28+
### 1. 检测项目API
29+
```bash
30+
mcpify detect /path/to/project --output config.json
31+
```
32+
33+
### 2. 查看配置文件
34+
```bash
35+
mcpify view config.json
36+
```
37+
38+
### 3. 验证配置文件
39+
```bash
40+
mcpify validate config.json
41+
```
42+
43+
### 4. 启动MCP服务器
44+
```bash
45+
# 启动stdio模式(默认)
46+
mcpify serve config.json
47+
48+
# 启动HTTP流式模式
49+
mcpify serve config.json --mode streamable-http --port 8080
50+
```
51+
52+
## 独立服务器脚本
53+
54+
项目提供了多种方式来启动MCP服务器:
55+
56+
### 方式1:使用独立脚本(推荐给MCP客户端)
57+
```bash
58+
# 使用bin目录下的独立脚本
59+
./bin/mcp-serve config.json
60+
./bin/mcp-serve config.json --mode streamable-http --port 8080
61+
```
62+
63+
### 方式2:通过mcpify CLI
64+
```bash
65+
# 使用mcpify命令
66+
mcpify serve config.json
67+
mcpify serve config.json --mode streamable-http --port 8080
68+
```
69+
70+
### 方式3:直接调用模块
71+
```bash
72+
# 直接调用Python模块
73+
python -m mcpify serve config.json
74+
python -m mcpify serve config.json --mode streamable-http --port 8080
75+
```
76+
77+
## 使用场景
78+
79+
### 场景1:开发和测试
80+
```bash
81+
# 1. 检测项目API
82+
mcpify detect my-project --output my-project.json
83+
84+
# 2. 查看生成的配置
85+
mcpify view my-project.json
86+
87+
# 3. 直接启动服务器进行测试
88+
mcpify serve my-project.json
89+
```
90+
91+
### 场景2:MCP客户端集成
92+
MCP客户端可以通过多种方式启动服务器:
93+
94+
```bash
95+
# 使用独立脚本(推荐)
96+
./bin/mcp-serve config.json
97+
98+
# 使用mcpify CLI
99+
mcpify serve config.json
100+
101+
# 使用模块调用
102+
python -m mcpify serve config.json
103+
```
104+
105+
### 场景3:生产部署
106+
```bash
107+
# 启动HTTP模式的服务器,监听特定端口
108+
./bin/mcp-serve config.json --mode streamable-http --host 0.0.0.0 --port 8080
109+
```
110+
111+
## 配置文件示例
112+
113+
查看 `examples/` 目录下的示例配置文件:
114+
115+
### FastAPI 后端示例
116+
```json
117+
{
118+
"name": "my-fastapi-server",
119+
"description": "FastAPI backend server",
120+
"backend": {
121+
"type": "fastapi",
122+
"base_url": "http://localhost:8000"
123+
},
124+
"tools": [
125+
{
126+
"name": "get_user",
127+
"description": "Get user information",
128+
"endpoint": "/users/{user_id}",
129+
"method": "GET",
130+
"parameters": [
131+
{
132+
"name": "user_id",
133+
"type": "string",
134+
"description": "User ID"
135+
}
136+
]
137+
}
138+
]
139+
}
140+
```
141+
142+
### Python 模块后端示例
143+
```json
144+
{
145+
"name": "my-python-module",
146+
"description": "Python module backend",
147+
"backend": {
148+
"type": "python",
149+
"module_path": "./my_module.py"
150+
},
151+
"tools": [
152+
{
153+
"name": "calculate",
154+
"description": "Perform calculation",
155+
"function": "calculate",
156+
"parameters": [
157+
{
158+
"name": "expression",
159+
"type": "string",
160+
"description": "Mathematical expression"
161+
}
162+
]
163+
}
164+
]
165+
}
166+
```
167+
168+
### 命令行工具后端示例
169+
```json
170+
{
171+
"name": "my-cli-tool",
172+
"description": "Command line tool backend",
173+
"backend": {
174+
"type": "commandline",
175+
"config": {
176+
"command": "python3",
177+
"args": ["./my_script.py"],
178+
"cwd": "."
179+
}
180+
},
181+
"tools": [
182+
{
183+
"name": "process_data",
184+
"description": "Process data with CLI tool",
185+
"args": ["process", "{input_file}"],
186+
"parameters": [
187+
{
188+
"name": "input_file",
189+
"type": "string",
190+
"description": "Input file path"
191+
}
192+
]
193+
}
194+
]
195+
}
196+
```
197+
198+
## 支持的后端类型
199+
200+
- `fastapi`: FastAPI应用
201+
- `python`: Python模块
202+
- `external`: 外部命令行工具
203+
- `commandline`: 简单命令行工具(向后兼容)
204+
205+
## 服务器模式
206+
207+
- `stdio`: 标准输入输出模式(MCP默认)
208+
- `streamable-http`: HTTP服务器端事件流模式
209+
210+
## 安装和部署
211+
212+
### 开发安装
213+
```bash
214+
pip install -e .
215+
```
216+
217+
### 生产安装
218+
```bash
219+
pip install mcpify
220+
```
221+
222+
### 使用独立脚本(无需安装)
223+
```bash
224+
# 复制bin/mcp-serve到目标位置
225+
cp bin/mcp-serve /usr/local/bin/
226+
chmod +x /usr/local/bin/mcp-serve
227+
# 然后可以直接使用
228+
mcp-serve config.json
229+
```
230+
231+
这种设计让MCP客户端可以完全控制服务器的启动时机和方式,同时提供了多种部署选项以适应不同的使用场景。

example-projects/.DS_Store

-6 KB
Binary file not shown.

examples/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# MCPify Configuration Examples
2+
3+
This directory contains example configuration files for different types of backends and use cases.
4+
5+
## Available Examples
6+
7+
### 1. FastAPI Backend (`fastapi-example.json`)
8+
Example configuration for a FastAPI web application backend.
9+
- **Backend Type**: `fastapi`
10+
- **Use Case**: REST API servers, web applications
11+
- **Features**: HTTP endpoints, automatic parameter validation
12+
13+
### 2. Python Module Backend (`python-module-example.json`)
14+
Example configuration for a Python module/script backend.
15+
- **Backend Type**: `python`
16+
- **Use Case**: Python functions, data processing scripts
17+
- **Features**: Direct function calls, Python-native integration
18+
19+
### 3. Command Line Tool Backend (`commandline-example.json`)
20+
Example configuration for command-line tools and external programs.
21+
- **Backend Type**: `commandline`
22+
- **Use Case**: CLI tools, shell scripts, external executables
23+
- **Features**: Process execution, argument templating
24+
25+
## Usage
26+
27+
To test any of these examples:
28+
29+
```bash
30+
# View the configuration
31+
mcpify view examples/fastapi-example.json
32+
33+
# Validate the configuration
34+
mcpify validate examples/fastapi-example.json
35+
36+
# Start the server (make sure the backend is running first)
37+
mcpify serve examples/fastapi-example.json
38+
39+
# Or use the standalone server
40+
mcp-serve examples/fastapi-example.json
41+
```
42+
43+
## Customization
44+
45+
These examples serve as templates. You can:
46+
47+
1. Copy an example that matches your backend type
48+
2. Modify the backend configuration (URLs, paths, commands)
49+
3. Update the tools section to match your API
50+
4. Add or remove parameters as needed
51+
52+
## Backend Requirements
53+
54+
- **FastAPI**: Requires a running FastAPI server
55+
- **Python**: Requires the Python module to be available
56+
- **Command Line**: Requires the command/executable to be installed
57+
58+
For more detailed usage instructions, see `docs/usage.md`.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

example-projects/python-cmd-tool/README.md renamed to examples/python-cmd-tool/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ Result: 10 + 20 = 30
7575
```
7676

7777
## License
78-
This project is licensed under the MIT License.
78+
This project is licensed under the MIT License.

0 commit comments

Comments
 (0)