@@ -44,28 +44,62 @@ 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"
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+ )
6157
62- # Register the tool with the server
63- mcp .tool (algorithm_tool )
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__ = (
93+ f"Execute { tool_name } algorithm with parameters dictionary"
94+ )
95+ mcp .tool (algorithm_tool )
6496
6597
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 } " )
98+ def setup_server (
99+ db_url : str , username : str , password : str , database : str = None
100+ ) -> FastMCP :
101+ """Set up the FastMCP server with all tools registered"""
102+ logger .info (f"Setting up MCP Server for { db_url } with username { username } " )
69103 if database :
70104 logger .info (f"Connecting to database: { database } " )
71105
@@ -142,6 +176,12 @@ async def list_tools() -> str:
142176 for tool_name in algorithm_names :
143177 create_algorithm_tool (mcp , tool_name , gds )
144178
179+ return mcp
180+
181+
182+ def main (db_url : str , username : str , password : str , database : str = None ):
183+ """Main function that sets up and runs the FastMCP server"""
184+ mcp = setup_server (db_url , username , password , database )
145185 # Run the server - FastMCP will handle the event loop
146186 mcp .run ()
147187
0 commit comments