33import tempfile
44from typing import Dict , Optional
55
6- import mcp .types as types
76from mcp import StdioServerParameters , stdio_client
7+ from mcp .shared .message import SessionMessage
8+ from mcp .types import JSONRPCError , JSONRPCMessage , JSONRPCRequest , JSONRPCResponse
89from opentelemetry import trace
910from uipath import UiPath
1011
@@ -118,8 +119,8 @@ async def _run_server(self, server_params: StdioServerParameters) -> None:
118119 try :
119120 while True :
120121 # Get message from local server
121- message = await self ._read_stream .receive ()
122-
122+ session_message = await self ._read_stream .receive ()
123+ message = session_message . message
123124 # For responses, determine which request_id to use
124125 if self ._is_response (message ):
125126 message_id = self ._get_message_id (message )
@@ -180,7 +181,7 @@ async def _consume_messages(self):
180181 logger .info (
181182 f"Session { self ._session_id } - processing queued message: { message } ..."
182183 )
183- await self ._write_stream .send (message )
184+ await self ._write_stream .send (SessionMessage ( message = message ) )
184185 except Exception as e :
185186 logger .error (
186187 f"Error processing message for session { self ._session_id } : { e } "
@@ -196,9 +197,7 @@ async def _consume_messages(self):
196197 except asyncio .QueueEmpty :
197198 break
198199
199- async def _send_message (
200- self , message : types .JSONRPCMessage , request_id : str
201- ) -> None :
200+ async def _send_message (self , message : JSONRPCMessage , request_id : str ) -> None :
202201 """Send new message to UiPath MCP Server."""
203202 with self ._mcp_tracer .create_span_for_message (
204203 message ,
@@ -224,7 +223,7 @@ async def _send_message(
224223 raise
225224
226225 async def _send_message_internal (
227- self , message : types . JSONRPCMessage , request_id : str
226+ self , message : JSONRPCMessage , request_id : str
228227 ) -> None :
229228 response = await self ._uipath .api_client .request_async (
230229 "POST" ,
@@ -246,7 +245,7 @@ async def _get_messages_internal(self, request_id: str) -> None:
246245 messages = response .json ()
247246 for message in messages :
248247 logger .info (f"Received message: { message } " )
249- json_message = types . JSONRPCMessage .model_validate (message )
248+ json_message = JSONRPCMessage .model_validate (message )
250249 if self ._is_request (json_message ):
251250 message_id = self ._get_message_id (json_message )
252251 if message_id :
@@ -261,23 +260,21 @@ async def _get_messages_internal(self, request_id: str) -> None:
261260 elif 500 <= response .status_code < 600 :
262261 raise Exception (f"{ response .status_code } - { response .text } " )
263262
264- def _is_request (self , message : types . JSONRPCMessage ) -> bool :
263+ def _is_request (self , message : JSONRPCMessage ) -> bool :
265264 """Check if a message is a JSONRPCRequest."""
266265 if hasattr (message , "root" ):
267266 root = message .root
268- return isinstance (root , types . JSONRPCRequest )
267+ return isinstance (root , JSONRPCRequest )
269268 return False
270269
271- def _is_response (self , message : types . JSONRPCMessage ) -> bool :
270+ def _is_response (self , message : JSONRPCMessage ) -> bool :
272271 """Check if a message is a JSONRPCResponse or JSONRPCError."""
273272 if hasattr (message , "root" ):
274273 root = message .root
275- return isinstance (root , types .JSONRPCResponse ) or isinstance (
276- root , types .JSONRPCError
277- )
274+ return isinstance (root , JSONRPCResponse ) or isinstance (root , JSONRPCError )
278275 return False
279276
280- def _get_message_id (self , message : types . JSONRPCMessage ) -> str :
277+ def _get_message_id (self , message : JSONRPCMessage ) -> str :
281278 """Extract the message id from a JSONRPCMessage."""
282279 if hasattr (message , "root" ) and hasattr (message .root , "id" ):
283280 return str (message .root .id )
0 commit comments