1010import logging
1111from quart import Quart , request , jsonify
1212from quart_cors import cors
13- import atexit
1413
1514# Configure logging
1615logging .basicConfig (level = logging .INFO )
1716logger = logging .getLogger (__name__ )
1817
19- # sample async function
20- # async def test():
21- # print("Hello, world!")
22- # await asyncio.sleep(1)
23- # print("Goodbye, world!")
24-
18+ # Define an MCP client
2519class MCPClient :
2620 def __init__ (self ):
2721 # Initialize session and client objects
@@ -46,7 +40,6 @@ async def connect_to_server(self, server_script_path: str):
4640 env = None
4741 )
4842 try :
49-
5043 logger .info ("Starting async context" )
5144 stdio_transport = await self .exit_stack .enter_async_context (stdio_client (server_params ))
5245 self .stdio , self .write = stdio_transport
@@ -101,12 +94,10 @@ async def process_query(self, query: str) -> str:
10194 tool_args = content .input
10295 logger .info (f"Noticed tool { tool_name } with args { tool_args } " )
10396
104-
10597 if self .session is None :
10698 logger .error ("Session not initialized. Exiting." )
10799 return jsonify ({"response" : "Session not initialized. Exiting." })
108100
109-
110101 try :
111102 # Execute tool call
112103 await self .session .initialize ()
@@ -146,7 +137,7 @@ async def cleanup(self):
146137 """Clean up resources"""
147138 await self .exit_stack .aclose ()
148139
149- # let's start a quart server
140+ # let's start a Quart server
150141app = Quart (__name__ )
151142app = cors (app , allow_origin = "*" )
152143
@@ -157,10 +148,8 @@ async def chat():
157148 data = await request .get_json ()
158149 query = data .get ('messages' , [None ])[0 ]
159150 logger .info (f"Received query: { query } " )
160- logger .info ("Anthropic API Key: " + os .getenv ("ANTHROPIC_API_KEY" ))
161151 await client .connect_to_server ("/app/.venv/bin/mcp-server-time" )
162152 return await client .process_query (query )
163- # await client.process_query("what is the time in tokyo")
164153 finally :
165154 await client .cleanup ()
166155
0 commit comments