2222import json
2323import logging
2424import uuid
25- from typing import Any , Dict , Optional , Set
25+ from typing import Any
2626
2727from .core .context import Context
2828from .registries .tools import tool
@@ -39,15 +39,15 @@ class CallbackManager:
3939 """
4040
4141 def __init__ (self ):
42- self .active_callbacks : Dict [str , Dict [str , Any ]] = {}
43- self .callback_permissions : Dict [str , Set [str ]] = {}
42+ self .active_callbacks : dict [str , dict [str , Any ]] = {}
43+ self .callback_permissions : dict [str , set [str ]] = {}
4444 self .callback_timeout = 300.0 # 5 minutes default
4545
4646 def register_callback (
4747 self ,
4848 callback_id : str ,
4949 context : Context ,
50- allowed_tools : Optional [ Set [ str ]] = None ,
50+ allowed_tools : set [ str ] | None = None ,
5151 ) -> None :
5252 """Register a callback session for R → Python communication."""
5353 self .active_callbacks [callback_id ] = {
@@ -66,8 +66,8 @@ def unregister_callback(self, callback_id: str) -> None:
6666 logger .info (f"Unregistered callback session: { callback_id } " )
6767
6868 async def handle_callback (
69- self , callback_id : str , tool_name : str , parameters : Dict [str , Any ]
70- ) -> Dict [str , Any ]:
69+ self , callback_id : str , tool_name : str , parameters : dict [str , Any ]
70+ ) -> dict [str , Any ]:
7171 """Handle a callback request from R."""
7272 if callback_id not in self .active_callbacks :
7373 return {
@@ -99,7 +99,6 @@ async def handle_callback(
9999 context = callback_info ["context" ]
100100
101101 # Import here to avoid circular imports
102- from .core .server import MCPServer
103102
104103 server = context ._server
105104
@@ -174,8 +173,8 @@ def get_callback_manager() -> CallbackManager:
174173 },
175174)
176175async def create_r_callback_session (
177- context : Context , params : Dict [str , Any ]
178- ) -> Dict [str , Any ]:
176+ context : Context , params : dict [str , Any ]
177+ ) -> dict [str , Any ]:
179178 """
180179 Create a callback session that allows R scripts to call back to Python MCP tools.
181180
@@ -235,7 +234,7 @@ async def create_r_callback_session(
235234 "required" : ["callback_id" , "tool_name" , "parameters" ],
236235 },
237236)
238- async def handle_r_callback (context : Context , params : Dict [str , Any ]) -> Dict [str , Any ]:
237+ async def handle_r_callback (context : Context , params : dict [str , Any ]) -> dict [str , Any ]:
239238 """
240239 Handle a callback request from an R script.
241240
@@ -269,14 +268,14 @@ def create_r_callback_utilities() -> str:
269268# Initialize callback system
270269rmcp_init_callback <- function(callback_config) {
271270 .rmcp_callback_config <<- callback_config
272-
271+
273272 # Create temporary file for callback communication
274273 .rmcp_callback_file <<- tempfile(pattern = "rmcp_callback_", fileext = ".json")
275-
274+
276275 cat("RMCP callback system initialized\\ n")
277276 cat("Callback ID:", callback_config$callback_id, "\\ n")
278277 cat("Allowed tools:", paste(callback_config$allowed_tools, collapse = ", "), "\\ n")
279-
278+
280279 return(TRUE)
281280}
282281
@@ -285,28 +284,28 @@ def create_r_callback_utilities() -> str:
285284 if (is.null(.rmcp_callback_config)) {
286285 stop("Callback system not initialized. Call rmcp_init_callback() first.")
287286 }
288-
287+
289288 # Check if tool is allowed
290- if (length(.rmcp_callback_config$allowed_tools) > 0 &&
289+ if (length(.rmcp_callback_config$allowed_tools) > 0 &&
291290 !tool_name %in% .rmcp_callback_config$allowed_tools) {
292291 stop(paste("Tool", tool_name, "not allowed for callbacks"))
293292 }
294-
293+
295294 # Prepare callback request
296295 callback_request <- list(
297296 callback_id = .rmcp_callback_config$callback_id,
298297 tool_name = tool_name,
299298 parameters = parameters,
300299 timestamp = as.numeric(Sys.time())
301300 )
302-
301+
303302 # Write request to file
304303 write(toJSON(callback_request, auto_unbox = TRUE), .rmcp_callback_file)
305-
304+
306305 # Signal Python to handle callback (this is a simplified version)
307306 # In practice, this would use a more sophisticated IPC mechanism
308307 cat("RMCP_CALLBACK:", .rmcp_callback_file, "\\ n", file = stderr())
309-
308+
310309 # For now, return a placeholder response
311310 # Real implementation would wait for Python response
312311 return(list(
@@ -327,10 +326,10 @@ def create_r_callback_utilities() -> str:
327326 timestamp = as.numeric(Sys.time()),
328327 callback_id = .rmcp_callback_config$callback_id
329328 )
330-
329+
331330 cat("RMCP_PROGRESS_CALLBACK:", toJSON(progress_data, auto_unbox = TRUE), "\\ n", file = stderr())
332331 }
333-
332+
334333 # Also call regular progress function
335334 if (exists("rmcp_progress")) {
336335 rmcp_progress(message, current, total)
@@ -359,10 +358,10 @@ def create_r_callback_utilities() -> str:
359358 if (!is.null(.rmcp_callback_file) && file.exists(.rmcp_callback_file)) {
360359 unlink(.rmcp_callback_file)
361360 }
362-
361+
363362 .rmcp_callback_config <<- NULL
364363 .rmcp_callback_file <<- NULL
365-
364+
366365 cat("RMCP callback system cleaned up\\ n")
367366}
368367
@@ -398,8 +397,8 @@ def create_r_callback_utilities() -> str:
398397 },
399398)
400399async def setup_r_bidirectional (
401- context : Context , params : Dict [str , Any ]
402- ) -> Dict [str , Any ]:
400+ context : Context , params : dict [str , Any ]
401+ ) -> dict [str , Any ]:
403402 """
404403 Set up bidirectional communication in an R session.
405404
@@ -428,19 +427,19 @@ async def setup_r_bidirectional(
428427 setup_script = f"""
429428 # Load RMCP callback utilities
430429 { create_r_callback_utilities ()}
431-
430+
432431 # Initialize callback system
433432 callback_config <- { json .dumps (callback_config )}
434433 rmcp_init_callback(callback_config)
435-
434+
436435 # Test callback system
437436 cat("Bidirectional communication ready\\ n")
438-
437+
439438 result <- list(
440439 bidirectional_enabled = TRUE,
441440 callback_id = callback_config$callback_id,
442441 allowed_tools = callback_config$allowed_tools,
443- session_id = "{ session_id or ' default' } "
442+ session_id = "{ session_id or " default" } "
444443 )
445444 """
446445
@@ -469,8 +468,8 @@ async def setup_r_bidirectional(
469468 input_schema = {"type" : "object" , "properties" : {}},
470469)
471470async def list_callback_sessions (
472- context : Context , params : Dict [str , Any ]
473- ) -> Dict [str , Any ]:
471+ context : Context , params : dict [str , Any ]
472+ ) -> dict [str , Any ]:
474473 """
475474 List all active callback sessions.
476475
0 commit comments