44import logging
55from contextlib import closing
66from 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
117from pydantic import AnyUrl
128from typing import Any
139
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+
1415# reconfigure UnicodeEncodeError prone default (i.e. windows-1252) to utf-8
1516if sys .platform == "win32" and os .environ .get ('PYTHONIOENCODING' ) is None :
1617 sys .stdin .reconfigure (encoding = "utf-8" )
101102Start your first message fully in character with something like "Oh, Hey there! I see you've chosen the topic {topic}. Let's get started! 🚀"
102103"""
103104
105+
104106class SqliteDatabase :
105107 def __init__ (self , db_path : str ):
106108 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
159161 logger .error (f"Database error executing query: { e } " )
160162 raise
161163
164+
162165async def main (db_path : str ):
163166 logger .info (f"Starting SQLite MCP Server with DB path: { db_path } " )
164167
@@ -213,7 +216,8 @@ async def handle_list_prompts() -> list[types.Prompt]:
213216
214217 @server .get_prompt ()
215218 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 } " )
217221 if name != "mcp-demo" :
218222 logger .error (f"Unknown prompt: { name } " )
219223 raise ValueError (f"Unknown prompt: { name } " )
@@ -231,7 +235,8 @@ async def handle_get_prompt(name: str, arguments: dict[str, str] | None) -> type
231235 messages = [
232236 types .PromptMessage (
233237 role = "user" ,
234- content = types .TextContent (type = "text" , text = prompt .strip ()),
238+ content = types .TextContent (
239+ type = "text" , text = prompt .strip ()),
235240 )
236241 ],
237242 )
@@ -342,19 +347,22 @@ async def handle_call_tool(
342347
343348 if name == "read_query" :
344349 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" )
346352 results = db ._execute_query (arguments ["query" ])
347353 return [types .TextContent (type = "text" , text = str (results ))]
348354
349355 elif name == "write_query" :
350356 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" )
352359 results = db ._execute_query (arguments ["query" ])
353360 return [types .TextContent (type = "text" , text = str (results ))]
354361
355362 elif name == "create_table" :
356363 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" )
358366 db ._execute_query (arguments ["query" ])
359367 return [types .TextContent (type = "text" , text = "Table created successfully" )]
360368
@@ -366,7 +374,7 @@ async def handle_call_tool(
366374 except Exception as e :
367375 return [types .TextContent (type = "text" , text = f"Error: { str (e )} " )]
368376
369- async with mcp . server . stdio . stdio_server () as (read_stream , write_stream ):
377+ async with stdio_server () as (read_stream , write_stream ):
370378 logger .info ("Server running with stdio transport" )
371379 await server .run (
372380 read_stream ,
@@ -380,3 +388,14 @@ async def handle_call_tool(
380388 ),
381389 ),
382390 )
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