@@ -44,28 +44,58 @@ def serialize_result(result: Any) -> str:
4444
4545
4646def create_algorithm_tool (mcp : FastMCP , tool_name : str , gds : GraphDataScience ):
47- """Create and register an algorithm tool with proper closure"""
48-
49- async def algorithm_tool (parameters : Dict [str , Any ] = None ) -> str :
50- """Execute algorithm tool with parameters dictionary"""
51- try :
52- handler = AlgorithmRegistry .get_handler (tool_name , gds )
53- result = handler .execute (parameters or {})
54- return serialize_result (result )
55- except Exception as e :
56- return f"Error executing { tool_name } : { str (e )} "
57-
58- # Set the function name and docstring
59- algorithm_tool .__name__ = tool_name
60- algorithm_tool .__doc__ = f"Execute { tool_name } algorithm with parameters dictionary"
61-
62- # Register the tool with the server
63- mcp .tool (algorithm_tool )
64-
65-
66- def main (db_url : str , username : str , password : str , database : str = None ):
67- """Main function that sets up and runs the FastMCP server"""
68- logger .info (f"Starting MCP Server for { db_url } with username { username } " )
47+ """Create and register an algorithm tool using a single parameters dict approach"""
48+ handler = AlgorithmRegistry .get_handler (tool_name , gds )
49+
50+ # Get tool definition from specs to extract parameter information
51+ all_tool_definitions = (
52+ centrality_tool_definitions
53+ + community_tool_definitions
54+ + path_tool_definitions
55+ + similarity_tool_definitions
56+ )
57+
58+ tool_def = None
59+ for tool in all_tool_definitions :
60+ if tool .name == tool_name :
61+ tool_def = tool
62+ break
63+
64+ if tool_def :
65+ # Create a function that accepts a single parameters dict
66+ # This is the only approach that works consistently with FastMCP
67+ async def algorithm_tool (parameters : Dict [str , Any ] = None ) -> str :
68+ """Execute algorithm tool with parameters dictionary"""
69+ try :
70+ result = handler .execute (parameters or {})
71+ return serialize_result (result )
72+ except Exception as e :
73+ return f"Error executing { tool_name } : { str (e )} "
74+
75+ # Set the function name and docstring
76+ algorithm_tool .__name__ = tool_name
77+ algorithm_tool .__doc__ = tool_def .description
78+
79+ # Register the tool with the server
80+ mcp .tool (algorithm_tool )
81+ else :
82+ # Fallback for tools without specs
83+ async def algorithm_tool (parameters : Dict [str , Any ] = None ) -> str :
84+ """Execute algorithm tool with parameters dictionary"""
85+ try :
86+ result = handler .execute (parameters or {})
87+ return serialize_result (result )
88+ except Exception as e :
89+ return f"Error executing { tool_name } : { str (e )} "
90+
91+ algorithm_tool .__name__ = tool_name
92+ algorithm_tool .__doc__ = f"Execute { tool_name } algorithm with parameters dictionary"
93+ mcp .tool (algorithm_tool )
94+
95+
96+ def setup_server (db_url : str , username : str , password : str , database : str = None ) -> FastMCP :
97+ """Set up the FastMCP server with all tools registered"""
98+ logger .info (f"Setting up MCP Server for { db_url } with username { username } " )
6999 if database :
70100 logger .info (f"Connecting to database: { database } " )
71101
@@ -142,6 +172,12 @@ async def list_tools() -> str:
142172 for tool_name in algorithm_names :
143173 create_algorithm_tool (mcp , tool_name , gds )
144174
175+ return mcp
176+
177+
178+ def main (db_url : str , username : str , password : str , database : str = None ):
179+ """Main function that sets up and runs the FastMCP server"""
180+ mcp = setup_server (db_url , username , password , database )
145181 # Run the server - FastMCP will handle the event loop
146182 mcp .run ()
147183
0 commit comments