Skip to content

Commit e3adb49

Browse files
author
sherlin
committed
update project framework
1 parent 908c17e commit e3adb49

File tree

6 files changed

+134
-107
lines changed

6 files changed

+134
-107
lines changed

README.md

Lines changed: 77 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,13 @@ The following figure illustrates the performance of the XiYan MCP server as meas
9898
Python 3.11+ is required.
9999
You can install the server through pip, and it will install the latest version:
100100

101-
```bash
101+
```shell
102102
pip install xiyan-mcp-server
103103
```
104104

105-
After that you can directly run the server by:
106-
```bash
107-
python -m xiyan_mcp_server
108-
```
109-
But it does not provide any functions until you complete following config.
110-
You will get a yml file. After that you can run the server by:
111-
```yaml
112-
env YML=path/to/yml python -m xiyan_mcp_server
105+
If you want to install the development version from source, you can install from source code on github:
106+
```shell
107+
pip install git+https://github.com/XGenerationLab/xiyan_mcp_server.git
113108
```
114109

115110
### Installing from Smithery.ai
@@ -123,11 +118,12 @@ You need a YAML config file to configure the server.
123118
A default config file is provided in config_demo.yml which looks like this:
124119

125120
```yaml
121+
mcp:
122+
transport: "stdio"
126123
model:
127124
name: "XGenerationLab/XiYanSQL-QwenCoder-32B-2412"
128125
key: ""
129126
url: "https://api-inference.modelscope.cn/v1/"
130-
131127
database:
132128
host: "localhost"
133129
port: 3306
@@ -136,6 +132,27 @@ database:
136132
database: ""
137133
```
138134
135+
### MCP Configuration
136+
You can set the transport protocol to ``stdio`` or ``sse``.
137+
#### STDIO
138+
For stdio protocol, you can set just like this:
139+
```yaml
140+
mcp:
141+
transport: "stdio"
142+
```
143+
#### SSE
144+
For sse protocol, you can set mcp config as below:
145+
```yaml
146+
mcp:
147+
transport: "sse"
148+
port: 8000
149+
log_level: "INFO"
150+
```
151+
The default port is `8000`. You can change the port if needed.
152+
The default log level is `ERROR`. We recommend to set log level to `INFO` for more detailed information.
153+
154+
Other configurations like `debug`, `host`, `sse_path`, `message_path` can be customized as well, but normally you don't need to modify them.
155+
139156
### LLM Configuration
140157
``Name`` is the name of the model to use, ``key`` is the API key of the model, ``url`` is the API url of the model. We support following models.
141158

@@ -201,53 +218,14 @@ model:
201218
name: "xiyansql-qwencoder-32b"
202219
key: "KEY"
203220
url: "https://xiyan-stream.biz.aliyun.com/service/api/xiyan-sql"
204-
database:
205221
```
206222

207223
Note: this model service is just for trial, if you need to use it in production, please contact us.
208224

225+
##### (3) Local version
209226
Alternatively, you can also deploy the model [XiYanSQL-qwencoder-32B](https://github.com/XGenerationLab/XiYanSQL-QwenCoder) on your own server.
227+
See [Local Model](src/xiyan_mcp_server/local_model/README.md) for more details.
210228

211-
#### Local Model
212-
Note: the local model is slow (about 12 seconds per query on my macbook).
213-
If you need a stable and fast service, we still recommend to use the modelscope version.
214-
215-
To run xiyan_mcp_server in local mode, you need
216-
1) a PC/Mac with at least 16GB RAM
217-
2) 6GB disk space
218-
219-
Step 1: Install additional Python packages
220-
```bash
221-
pip install flask modelscope torch==2.2.2 accelerate>=0.26.0 numpy=2.2.3
222-
```
223-
224-
Step 2: (optional) manually download the model
225-
We recommend [xiyansql-qwencoder-3b](https://www.modelscope.cn/models/XGenerationLab/XiYanSQL-QwenCoder-3B-2502/).
226-
You can manually download the model by
227-
```bash
228-
modelscope download --model XGenerationLab/XiYanSQL-QwenCoder-3B-2502
229-
```
230-
It will take you 6GB disk space.
231-
232-
Step 3: download the script and run server. src/xiyan_mcp_server/local_xiyan_server.py
233-
234-
235-
236-
```bash
237-
python local_xiyan_server.py
238-
```
239-
The server will be running on http://localhost:5090/
240-
241-
Step 4: prepare config and run xiyan_mcp_server
242-
the config.yml should be like:
243-
```yml
244-
model:
245-
name: "xiyansql-qwencoder-3b"
246-
key: "KEY"
247-
url: "http://127.0.0.1:5090"
248-
```
249-
250-
Till now the local mode is ready.
251229

252230
### Database Configuration
253231
``host``, ``port``, ``user``, ``password``, ``database`` are the connection information of the database.
@@ -283,8 +261,19 @@ database:
283261
Note that ``dialect`` should be ``postgresql`` for postgresql.
284262
## Launch
285263

286-
### STDIO Transport
287-
Normally, stdio transport is used with the specific mcp application as follows.
264+
### Server Launch
265+
266+
If you want to launch server with `sse`, you have to run the following command in a terminal:
267+
```shell
268+
YML=path/to/yml python -m xiyan_mcp_server
269+
```
270+
Then you should see the information on http://localhost:8000/sse in your browser. (Defaultly, change if your mcp server runs on other host/port)
271+
272+
Otherwise, if you use `stdio` transport protocol, you usually declare the mcp server command in specific mcp application instead of launching it in a terminal.
273+
However, you can still debug with this command if needed.
274+
275+
### Client Setting
276+
288277
#### Claude Desktop
289278
Add this in your Claude Desktop config file, ref <a href="https://github.com/XGenerationLab/xiyan_mcp_server/blob/main/imgs/claude_desktop.jpg">Claude Desktop config example</a>
290279
```json
@@ -304,17 +293,48 @@ Add this in your Claude Desktop config file, ref <a href="https://github.com/XGe
304293
}
305294
```
306295
**Please note that the Python command here requires the complete path to the Python executable (`/xxx/python`); otherwise, the Python interpreter cannot be found. You can determine this path by using the command `which python`. The same applies to other applications as well.**
296+
297+
Claude Desktop currently does not support the SSE transport protocol.
298+
307299
#### Cline
308300
Prepare the config like [Claude Desktop](#claude-desktop)
309301

310302
#### Goose
311-
Add following command in the config, ref <a href="https://github.com/XGenerationLab/xiyan_mcp_server/blob/main/imgs/goose.jpg">Goose config example</a>
312-
313-
```yaml
303+
If you use `stdio`, add following command in the config, ref <a href="https://github.com/XGenerationLab/xiyan_mcp_server/blob/main/imgs/goose.jpg">Goose config example</a>
304+
```shell
314305
env YML=path/to/yml /xxx/python -m xiyan_mcp_server
315306
```
307+
Otherwise, if you use `sse`, change Type to `SSE` and set the endpoint to `http://127.0.0.1:8000/sse`
316308
#### Cursor
317-
Use the same command like [Goose](#goose).
309+
Use the similar command as follows.
310+
311+
For `stdio`:
312+
```json
313+
{
314+
"mcpServers": {
315+
"xiyan-mcp-server": {
316+
"command": "/xxx/python",
317+
"args": [
318+
"-m",
319+
"xiyan_mcp_server"
320+
],
321+
"env": {
322+
"YML": "path/to/yml"
323+
}
324+
}
325+
}
326+
}
327+
```
328+
For `sse`:
329+
```json
330+
{
331+
"mcpServers": {
332+
"xiyan_mcp_server_1": {
333+
"url": "http://localhost:8000/sse"
334+
}
335+
}
336+
}
337+
```
318338

319339

320340
#### Witsy
@@ -325,25 +345,6 @@ Add following in command:
325345
Add an env: key is YML and value is the path to your yml file.
326346
Ref <a href="https://github.com/XGenerationLab/xiyan_mcp_server/blob/main/imgs/witsy.jpg">Witsy config example</a>
327347

328-
### SSE Transport
329-
SSE transport server has to be started with the following command first. The server runs defaultly on port 8000. You can change it by setting the environment variable FASTMCP_PORT.
330-
```shell
331-
env YML=path/to/yml FASTMCP_PORT=8000 python -m xiyan_mcp_server sse
332-
```
333-
You may also change the host or other parameters by setting the corresponding environment variables, but these are not fully tested.
334-
Then you should see the information on http://localhost:8000/sse in your browser.
335-
336-
Then you can use the server in applications mentioned above. For example, the config of Cursor is like this:
337-
```json
338-
{
339-
"mcpServers": {
340-
"xiyan-mcp-server": {
341-
"url": "http://127.0.0.1:8000/sse"
342-
}
343-
}
344-
}
345-
```
346-
347348

348349
## It Does Not Work!
349350
Contact us:

src/xiyan_mcp_server/__init__.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +0,0 @@
1-
import argparse
2-
3-
from .server import mcp
4-
5-
6-
7-
def main():
8-
parser = argparse.ArgumentParser(description="Run MCP server.")
9-
parser.add_argument('transport', nargs='?', default='stdio', choices=['stdio', 'sse'],
10-
help='Transport type (stdio or sse)')
11-
args = parser.parse_args()
12-
mcp.run(transport=args.transport)
13-
14-
if __name__ == "__main__":
15-
main()

src/xiyan_mcp_server/__main__.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import argparse
2-
import sys
3-
4-
from .server import mcp
51

2+
from .server import mcp, global_config, mcp_config
63

74
def main():
8-
parser = argparse.ArgumentParser(description="Run MCP server.")
9-
parser.add_argument('transport', nargs='?', default='stdio', choices=['stdio', 'sse'],
10-
help='Transport type (stdio or sse)')
11-
args = parser.parse_args(sys.argv[1:]) # 忽略第一个参数(模块名)
12-
mcp.run(transport=args.transport)
5+
mcp.run(transport=mcp_config.get('transport', 'stdio'))
6+
7+
138

149
if __name__ == "__main__":
1510
main()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Local Model Configuration
2+
Note: the local model is slow (about 12 seconds per query on my macbook).
3+
If you need a stable and fast service, we still recommend to use the modelscope version.
4+
5+
To run xiyan_mcp_server in local mode, you need
6+
1) a PC/Mac/Machine with at least 16GB RAM
7+
2) 6GB disk space
8+
9+
The above setting is for model of size 3B. You can adjust the settings to run a 32B model on a server.
10+
11+
### Step 1: Install additional Python packages
12+
```bash
13+
pip install flask modelscope torch==2.2.2 accelerate>=0.26.0 numpy=2.2.3
14+
```
15+
16+
### Step 2: (optional) manually download the model
17+
We recommend [xiyansql-qwencoder-3b](https://www.modelscope.cn/models/XGenerationLab/XiYanSQL-QwenCoder-3B-2502/).
18+
You can manually download the model by
19+
```bash
20+
modelscope download --model XGenerationLab/XiYanSQL-QwenCoder-3B-2502
21+
```
22+
It will take you 6GB disk space.
23+
24+
### Step 3: download the script and run server.
25+
26+
Script is located at `src/xiyan_mcp_server/local_model/local_xiyan_server.py`
27+
28+
```bash
29+
python local_xiyan_server.py
30+
```
31+
The server will be running on http://localhost:5090/
32+
33+
### Step 4: prepare config and run xiyan_mcp_server
34+
the config.yml should be like:
35+
```yml
36+
model:
37+
name: "xiyansql-qwencoder-3b"
38+
key: "KEY"
39+
url: "http://127.0.0.1:5090"
40+
```
41+
42+
Till now the local model is ready.

src/xiyan_mcp_server/server.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
import logging
33
import os
44
from typing import Literal
5-
65
import yaml # 添加yaml库导入
76

87
from mysql.connector import connect, Error
98
from mcp.server import FastMCP
109
from mcp.types import TextContent
10+
1111
from .utils.db_config import DBConfig
1212
from .database_env import DataBaseEnv
1313
from .utils.db_source import HITLSQLDatabase
1414
from .utils.db_util import init_db_conn
1515
from .utils.file_util import extract_sql_from_qwen
1616
from .utils.llm_util import call_openai_sdk
1717

18-
mcp = FastMCP("xiyan")
18+
1919

2020

2121
# Configure logging
@@ -25,9 +25,10 @@
2525
)
2626
logger = logging.getLogger("xiyan_mcp_server")
2727

28+
2829
def get_yml_config():
2930
config_path = os.getenv("YML", os.path.join(os.path.dirname(__file__), "config_demo.yml"))
30-
print(f"Load configuration from {config_path}")
31+
logger.info(f"Loading configuration from {config_path}")
3132
try:
3233
with open(config_path, 'r') as file:
3334
config = yaml.safe_load(file)
@@ -48,15 +49,18 @@ def get_xiyan_config(db_config):
4849

4950

5051
global_config = get_yml_config()
51-
#print(global_config)
52+
mcp_config = global_config.get('mcp', {})
5253
model_config = global_config['model']
5354
global_db_config = global_config.get('database')
54-
database_name = global_db_config.get('database','')
5555
global_xiyan_db_config = get_xiyan_config(global_db_config)
5656
dialect = global_db_config.get('dialect','mysql')
57-
#print("dialect is !!!!"+dialect)
5857

59-
@mcp.resource(dialect+'://'+database_name)
58+
59+
60+
mcp = FastMCP("xiyan", **mcp_config)
61+
62+
63+
@mcp.resource(dialect+'://'+global_db_config.get('database',''))
6064
async def read_resource() -> str:
6165

6266
db_engine = init_db_conn(global_xiyan_db_config)

0 commit comments

Comments
 (0)