update readme

This commit is contained in:
Gaoqiang Ji
2025-04-16 16:46:22 +08:00
parent b3961e050d
commit a8f84ccd76
2 changed files with 14 additions and 16 deletions

View File

@@ -111,6 +111,12 @@ Docker:
docker build -t mcp/sqlite . docker build -t mcp/sqlite .
``` ```
## Test with MCP inspector
```bash
mcp dev main.py:server
```
## License ## License
This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository. This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.

View File

@@ -12,6 +12,7 @@ from mcp.server.lowlevel import Server, NotificationOptions
from mcp.server.stdio import stdio_server from mcp.server.stdio import stdio_server
import mcp.types as types import mcp.types as types
# reconfigure UnicodeEncodeError prone default (i.e. windows-1252) to utf-8 # reconfigure UnicodeEncodeError prone default (i.e. windows-1252) to utf-8
if sys.platform == "win32" and os.environ.get('PYTHONIOENCODING') is None: if sys.platform == "win32" and os.environ.get('PYTHONIOENCODING') is None:
sys.stdin.reconfigure(encoding="utf-8") sys.stdin.reconfigure(encoding="utf-8")
@@ -102,7 +103,6 @@ The provided XML tags are for the assistants understanding. Implore to make all
Start your first message fully in character with something like "Oh, Hey there! I see you've chosen the topic {topic}. Let's get started! 🚀" Start your first message fully in character with something like "Oh, Hey there! I see you've chosen the topic {topic}. Let's get started! 🚀"
""" """
class SqliteDatabase: class SqliteDatabase:
def __init__(self, db_path: str): def __init__(self, db_path: str):
self.db_path = str(Path(db_path).expanduser()) self.db_path = str(Path(db_path).expanduser())
@@ -161,7 +161,6 @@ class SqliteDatabase:
logger.error(f"Database error executing query: {e}") logger.error(f"Database error executing query: {e}")
raise raise
async def main(db_path: str): async def main(db_path: str):
logger.info(f"Starting SQLite MCP Server with DB path: {db_path}") logger.info(f"Starting SQLite MCP Server with DB path: {db_path}")
@@ -216,8 +215,7 @@ async def main(db_path: str):
@server.get_prompt() @server.get_prompt()
async def handle_get_prompt(name: str, arguments: dict[str, str] | None) -> types.GetPromptResult: async def handle_get_prompt(name: str, arguments: dict[str, str] | None) -> types.GetPromptResult:
logger.debug( logger.debug(f"Handling get_prompt request for {name} with args {arguments}")
f"Handling get_prompt request for {name} with args {arguments}")
if name != "mcp-demo": if name != "mcp-demo":
logger.error(f"Unknown prompt: {name}") logger.error(f"Unknown prompt: {name}")
raise ValueError(f"Unknown prompt: {name}") raise ValueError(f"Unknown prompt: {name}")
@@ -235,8 +233,7 @@ async def main(db_path: str):
messages=[ messages=[
types.PromptMessage( types.PromptMessage(
role="user", role="user",
content=types.TextContent( content=types.TextContent(type="text", text=prompt.strip()),
type="text", text=prompt.strip()),
) )
], ],
) )
@@ -347,22 +344,19 @@ async def main(db_path: str):
if name == "read_query": if name == "read_query":
if not arguments["query"].strip().upper().startswith("SELECT"): if not arguments["query"].strip().upper().startswith("SELECT"):
raise ValueError( raise ValueError("Only SELECT queries are allowed for read_query")
"Only SELECT queries are allowed for read_query")
results = db._execute_query(arguments["query"]) results = db._execute_query(arguments["query"])
return [types.TextContent(type="text", text=str(results))] return [types.TextContent(type="text", text=str(results))]
elif name == "write_query": elif name == "write_query":
if arguments["query"].strip().upper().startswith("SELECT"): if arguments["query"].strip().upper().startswith("SELECT"):
raise ValueError( raise ValueError("SELECT queries are not allowed for write_query")
"SELECT queries are not allowed for write_query")
results = db._execute_query(arguments["query"]) results = db._execute_query(arguments["query"])
return [types.TextContent(type="text", text=str(results))] return [types.TextContent(type="text", text=str(results))]
elif name == "create_table": elif name == "create_table":
if not arguments["query"].strip().upper().startswith("CREATE TABLE"): if not arguments["query"].strip().upper().startswith("CREATE TABLE"):
raise ValueError( raise ValueError("Only CREATE TABLE statements are allowed")
"Only CREATE TABLE statements are allowed")
db._execute_query(arguments["query"]) db._execute_query(arguments["query"])
return [types.TextContent(type="text", text="Table created successfully")] return [types.TextContent(type="text", text="Table created successfully")]
@@ -374,7 +368,7 @@ async def main(db_path: str):
except Exception as e: except Exception as e:
return [types.TextContent(type="text", text=f"Error: {str(e)}")] return [types.TextContent(type="text", text=f"Error: {str(e)}")]
async with stdio_server() as (read_stream, write_stream): async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
logger.info("Server running with stdio transport") logger.info("Server running with stdio transport")
await server.run( await server.run(
read_stream, read_stream,
@@ -389,9 +383,7 @@ async def main(db_path: str):
), ),
) )
class ServerWrapper(): class ServerWrapper():
"""A helper class which allows you to go with `mcp run` or `mcp dev`"""
async def run(self): async def run(self):
import asyncio import asyncio