Skip to content

Commit 70063d2

Browse files
Merge pull request #14 from ZhuangbilityY/main
add sse transport
2 parents 1970e01 + e3adb49 commit 70063d2

File tree

8 files changed

+160
-84
lines changed

8 files changed

+160
-84
lines changed

README.md

Lines changed: 87 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
🌐 <a href="https://github.com/XGenerationLab/XiYan-SQL" >XiYan-SQL</a> |
1515
📖 <a href="https://arxiv.org/abs/2411.08599"> Arxiv</a> |
1616
📄 <a href="https://paperswithcode.com/paper/xiyan-sql-a-multi-generator-ensemble" >PapersWithCode</a>
17-
💻 <a href="https://huggingface.co/collections/XGenerationLab/xiyansql-models-67c9844307b49f87436808fc">HuggingFace</a> |
17+
🤗 <a href="https://huggingface.co/collections/XGenerationLab/xiyansql-models-67c9844307b49f87436808fc">HuggingFace</a> |
1818
🤖 <a href="https://modelscope.cn/collections/XiYanSQL-Models-4483337b614241" >ModelScope</a> |
1919
🌕 <a href="https://bailian.console.aliyun.com/xiyan">析言GBI</a>
2020
<br />
@@ -100,18 +100,13 @@ The following figure illustrates the performance of the XiYan MCP server as meas
100100
Python 3.11+ is required.
101101
You can install the server through pip, and it will install the latest version:
102102

103-
```bash
103+
```shell
104104
pip install xiyan-mcp-server
105105
```
106106

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

117112
### Installing from Smithery.ai
@@ -125,11 +120,12 @@ You need a YAML config file to configure the server.
125120
A default config file is provided in config_demo.yml which looks like this:
126121

127122
```yaml
123+
mcp:
124+
transport: "stdio"
128125
model:
129126
name: "XGenerationLab/XiYanSQL-QwenCoder-32B-2412"
130127
key: ""
131128
url: "https://api-inference.modelscope.cn/v1/"
132-
133129
database:
134130
host: "localhost"
135131
port: 3306
@@ -138,6 +134,27 @@ database:
138134
database: ""
139135
```
140136
137+
### MCP Configuration
138+
You can set the transport protocol to ``stdio`` or ``sse``.
139+
#### STDIO
140+
For stdio protocol, you can set just like this:
141+
```yaml
142+
mcp:
143+
transport: "stdio"
144+
```
145+
#### SSE
146+
For sse protocol, you can set mcp config as below:
147+
```yaml
148+
mcp:
149+
transport: "sse"
150+
port: 8000
151+
log_level: "INFO"
152+
```
153+
The default port is `8000`. You can change the port if needed.
154+
The default log level is `ERROR`. We recommend to set log level to `INFO` for more detailed information.
155+
156+
Other configurations like `debug`, `host`, `sse_path`, `message_path` can be customized as well, but normally you don't need to modify them.
157+
141158
### LLM Configuration
142159
``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.
143160

@@ -203,53 +220,14 @@ model:
203220
name: "xiyansql-qwencoder-32b"
204221
key: "KEY"
205222
url: "https://xiyan-stream.biz.aliyun.com/service/api/xiyan-sql"
206-
database:
207223
```
208224

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

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

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

254232
### Database Configuration
255233
``host``, ``port``, ``user``, ``password``, ``database`` are the connection information of the database.
@@ -284,7 +262,21 @@ database:
284262

285263
Note that ``dialect`` should be ``postgresql`` for postgresql.
286264
## Launch
287-
### Claude Desktop
265+
266+
### Server Launch
267+
268+
If you want to launch server with `sse`, you have to run the following command in a terminal:
269+
```shell
270+
YML=path/to/yml python -m xiyan_mcp_server
271+
```
272+
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)
273+
274+
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.
275+
However, you can still debug with this command if needed.
276+
277+
### Client Setting
278+
279+
#### Claude Desktop
288280
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>
289281
```json
290282
{
@@ -303,26 +295,59 @@ Add this in your Claude Desktop config file, ref <a href="https://github.com/XGe
303295
}
304296
```
305297
**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.**
306-
### Cline
307-
Prepare the config like [Claude Desktop](#claude-desktop)
308298

309-
### Goose
310-
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>
299+
Claude Desktop currently does not support the SSE transport protocol.
311300

312-
```yaml
301+
#### Cline
302+
Prepare the config like [Claude Desktop](#claude-desktop)
303+
304+
#### Goose
305+
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>
306+
```shell
313307
env YML=path/to/yml /xxx/python -m xiyan_mcp_server
314308
```
315-
### Cursor
316-
Use the same command like [Goose](#goose).
309+
Otherwise, if you use `sse`, change Type to `SSE` and set the endpoint to `http://127.0.0.1:8000/sse`
310+
#### Cursor
311+
Use the similar command as follows.
312+
313+
For `stdio`:
314+
```json
315+
{
316+
"mcpServers": {
317+
"xiyan-mcp-server": {
318+
"command": "/xxx/python",
319+
"args": [
320+
"-m",
321+
"xiyan_mcp_server"
322+
],
323+
"env": {
324+
"YML": "path/to/yml"
325+
}
326+
}
327+
}
328+
}
329+
```
330+
For `sse`:
331+
```json
332+
{
333+
"mcpServers": {
334+
"xiyan_mcp_server_1": {
335+
"url": "http://localhost:8000/sse"
336+
}
337+
}
338+
}
339+
```
317340

318341

319-
### Witsy
342+
#### Witsy
320343
Add following in command:
321-
```yaml
344+
```shell
322345
/xxx/python -m xiyan_mcp_server
323346
```
324347
Add an env: key is YML and value is the path to your yml file.
325348
Ref <a href="https://github.com/XGenerationLab/xiyan_mcp_server/blob/main/imgs/witsy.jpg">Witsy config example</a>
349+
350+
326351
## It Does Not Work!
327352
Contact us:
328353
<a href="https://github.com/XGenerationLab/xiyan_mcp_server/blob/main/imgs/dinggroup_out.png">Ding Group钉钉群</a>|

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "xiyan_mcp_server"
3-
version = "0.1.4"
3+
version = "0.1.5.dev0"
44
description = "A Model Context Protocol (MCP) server that utilizes XiyanSQL with databases. This server enables AI assistants to list tables, read data, and execute natural language queries"
55
readme = "README.md"
66
requires-python = ">=3.11"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='xiyan_mcp_server', # 包的名字
5-
version='0.1.2', # 版本号
5+
version='0.1.5.dev0', # 版本号
66
packages=find_packages(), # 自动找到项目中的包
77
install_requires=[ # 必要的包依赖
88
# 'numpy', # 示例:若有依赖包,将其列在此

src/xiyan_mcp_server/__init__.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
from . import server
2-
3-
def main():
4-
"""Main entry point for the package."""
5-
server.main() # 修改为调用server.py中的main函数
6-
7-
# Expose important items at package level
8-
__all__ = ['main', 'server']

src/xiyan_mcp_server/__main__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
from . import main
1+
2+
from .server import mcp, global_config, mcp_config
3+
4+
def main():
5+
mcp.run(transport=mcp_config.get('transport', 'stdio'))
6+
7+
28

39
if __name__ == "__main__":
410
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: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1+
import argparse
12
import logging
23
import os
4+
from typing import Literal
35
import yaml # 添加yaml库导入
46

57
from mysql.connector import connect, Error
68
from mcp.server import FastMCP
79
from mcp.types import TextContent
10+
811
from .utils.db_config import DBConfig
912
from .database_env import DataBaseEnv
1013
from .utils.db_source import HITLSQLDatabase
1114
from .utils.db_util import init_db_conn
1215
from .utils.file_util import extract_sql_from_qwen
1316
from .utils.llm_util import call_openai_sdk
1417

15-
mcp = FastMCP("xiyan")
18+
1619

1720

1821
# Configure logging
@@ -22,8 +25,10 @@
2225
)
2326
logger = logging.getLogger("xiyan_mcp_server")
2427

28+
2529
def get_yml_config():
2630
config_path = os.getenv("YML", os.path.join(os.path.dirname(__file__), "config_demo.yml"))
31+
logger.info(f"Loading configuration from {config_path}")
2732
try:
2833
with open(config_path, 'r') as file:
2934
config = yaml.safe_load(file)
@@ -44,15 +49,18 @@ def get_xiyan_config(db_config):
4449

4550

4651
global_config = get_yml_config()
47-
#print(global_config)
52+
mcp_config = global_config.get('mcp', {})
4853
model_config = global_config['model']
4954
global_db_config = global_config.get('database')
50-
database_name = global_db_config.get('database','')
5155
global_xiyan_db_config = get_xiyan_config(global_db_config)
5256
dialect = global_db_config.get('dialect','mysql')
53-
#print("dialect is !!!!"+dialect)
5457

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

5866
db_engine = init_db_conn(global_xiyan_db_config)
@@ -76,7 +84,7 @@ async def read_resource(table_name) -> str:
7684
raise RuntimeError(f"Database error: {str(e)}")
7785

7886

79-
def sql_gen_and_execute(db_env, query: str):
87+
def sql_gen_and_execute(db_env: DataBaseEnv, query: str):
8088
"""
8189
Transfers the input natural language question to sql query (known as Text-to-sql) and executes it on the database.
8290
Args:
@@ -174,17 +182,20 @@ def get_data(query: str)-> list[TextContent]:
174182
"""Fetch the data from database through a natural language query
175183
176184
Args:
177-
query: The query in natual language
185+
query: The query in natural language
178186
"""
179187

180188
res=call_xiyan(query)
181189
return [TextContent(type="text", text=res)]
182190

183-
def main():
184-
mcp.run()
185191

186192

187-
if __name__ == "__main__":
193+
def main():
194+
parser = argparse.ArgumentParser(description="Run MCP server.")
195+
parser.add_argument('transport', nargs='?', default='stdio', choices=['stdio', 'sse'],
196+
help='Transport type (stdio or sse)')
197+
args = parser.parse_args()
198+
mcp.run(transport=args.transport)
188199

200+
if __name__ == "__main__":
189201
main()
190-

0 commit comments

Comments
 (0)