4
4
import logging
5
5
from contextlib import closing
6
6
from pathlib import Path
7
- from mcp .server .models import InitializationOptions
8
- import mcp .types as types
9
- from mcp .server import NotificationOptions , Server
10
- import mcp .server .stdio
11
7
from pydantic import AnyUrl
12
8
from typing import Any
13
9
10
+ from mcp .server import InitializationOptions
11
+ from mcp .server .lowlevel import Server , NotificationOptions
12
+ from mcp .server .stdio import stdio_server
13
+ import mcp .types as types
14
+
14
15
# reconfigure UnicodeEncodeError prone default (i.e. windows-1252) to utf-8
15
16
if sys .platform == "win32" and os .environ .get ('PYTHONIOENCODING' ) is None :
16
17
sys .stdin .reconfigure (encoding = "utf-8" )
101
102
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! 🚀"
102
103
"""
103
104
105
+
104
106
class SqliteDatabase :
105
107
def __init__ (self , db_path : str ):
106
108
self .db_path = str (Path (db_path ).expanduser ())
@@ -159,6 +161,7 @@ def _execute_query(self, query: str, params: dict[str, Any] | None = None) -> li
159
161
logger .error (f"Database error executing query: { e } " )
160
162
raise
161
163
164
+
162
165
async def main (db_path : str ):
163
166
logger .info (f"Starting SQLite MCP Server with DB path: { db_path } " )
164
167
@@ -213,7 +216,8 @@ async def handle_list_prompts() -> list[types.Prompt]:
213
216
214
217
@server .get_prompt ()
215
218
async def handle_get_prompt (name : str , arguments : dict [str , str ] | None ) -> types .GetPromptResult :
216
- logger .debug (f"Handling get_prompt request for { name } with args { arguments } " )
219
+ logger .debug (
220
+ f"Handling get_prompt request for { name } with args { arguments } " )
217
221
if name != "mcp-demo" :
218
222
logger .error (f"Unknown prompt: { name } " )
219
223
raise ValueError (f"Unknown prompt: { name } " )
@@ -231,7 +235,8 @@ async def handle_get_prompt(name: str, arguments: dict[str, str] | None) -> type
231
235
messages = [
232
236
types .PromptMessage (
233
237
role = "user" ,
234
- content = types .TextContent (type = "text" , text = prompt .strip ()),
238
+ content = types .TextContent (
239
+ type = "text" , text = prompt .strip ()),
235
240
)
236
241
],
237
242
)
@@ -342,19 +347,22 @@ async def handle_call_tool(
342
347
343
348
if name == "read_query" :
344
349
if not arguments ["query" ].strip ().upper ().startswith ("SELECT" ):
345
- raise ValueError ("Only SELECT queries are allowed for read_query" )
350
+ raise ValueError (
351
+ "Only SELECT queries are allowed for read_query" )
346
352
results = db ._execute_query (arguments ["query" ])
347
353
return [types .TextContent (type = "text" , text = str (results ))]
348
354
349
355
elif name == "write_query" :
350
356
if arguments ["query" ].strip ().upper ().startswith ("SELECT" ):
351
- raise ValueError ("SELECT queries are not allowed for write_query" )
357
+ raise ValueError (
358
+ "SELECT queries are not allowed for write_query" )
352
359
results = db ._execute_query (arguments ["query" ])
353
360
return [types .TextContent (type = "text" , text = str (results ))]
354
361
355
362
elif name == "create_table" :
356
363
if not arguments ["query" ].strip ().upper ().startswith ("CREATE TABLE" ):
357
- raise ValueError ("Only CREATE TABLE statements are allowed" )
364
+ raise ValueError (
365
+ "Only CREATE TABLE statements are allowed" )
358
366
db ._execute_query (arguments ["query" ])
359
367
return [types .TextContent (type = "text" , text = "Table created successfully" )]
360
368
@@ -366,7 +374,7 @@ async def handle_call_tool(
366
374
except Exception as e :
367
375
return [types .TextContent (type = "text" , text = f"Error: { str (e )} " )]
368
376
369
- async with mcp . server . stdio . stdio_server () as (read_stream , write_stream ):
377
+ async with stdio_server () as (read_stream , write_stream ):
370
378
logger .info ("Server running with stdio transport" )
371
379
await server .run (
372
380
read_stream ,
@@ -380,3 +388,14 @@ async def handle_call_tool(
380
388
),
381
389
),
382
390
)
391
+
392
+
393
+ class ServerWrapper ():
394
+ """A helper class which allows you to go with `mcp run` or `mcp dev`"""
395
+
396
+ async def run (self ):
397
+ import asyncio
398
+ asyncio .run (main ("test.db" ))
399
+
400
+
401
+ server = ServerWrapper ()
0 commit comments