calculate_fibonacci
Compute the nth Fibonacci number from the sequence to analyze complex mathematical operations. Use this tool for precise Fibonacci calculations with detailed results.
Instructions
Calculate the nth Fibonacci number.
This is a more computationally intensive example that demonstrates
how tools can handle more complex operations.
Args:
n: The position in the Fibonacci sequence (must be >= 0)
Returns:
Dictionary containing the Fibonacci number and calculation info
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| n | Yes |
Implementation Reference
- mcp_ahrefs/tools/example_tools.py:61-92 (handler)The main handler function for the calculate_fibonacci tool. It computes the nth Fibonacci number iteratively, validates input (n >= 0), measures computation time, and returns a dictionary with position, value, and timing info.async def calculate_fibonacci(n: int) -> Dict[str, Any]: """Calculate the nth Fibonacci number. This is a more computationally intensive example that demonstrates how tools can handle more complex operations. Args: n: The position in the Fibonacci sequence (must be >= 0) Returns: Dictionary containing the Fibonacci number and calculation info """ if n < 0: raise ValueError("n must be non-negative") if n <= 1: return {"position": n, "value": n, "calculation_time": 0} start_time = time.time() # Calculate Fibonacci number a, b = 0, 1 for _ in range(2, n + 1): a, b = b, a + b calculation_time = time.time() - start_time return { "position": n, "value": b, "calculation_time": calculation_time }
- mcp_ahrefs/server/app.py:98-111 (registration)Registration loop in register_tools() that applies decorators (exception_handler, tool_logger) to each tool in example_tools (including calculate_fibonacci) and registers them explicitly with mcp_server.tool(name=tool_name).for tool_func in example_tools: # Apply SAAGA decorator chain: exception_handler → tool_logger decorated_func = exception_handler(tool_logger(tool_func, config.__dict__)) # Extract metadata from the original function tool_name = tool_func.__name__ # Register the decorated function directly with MCP # This preserves the function signature for parameter introspection mcp_server.tool( name=tool_name )(decorated_func) unified_logger.info(f"Registered tool: {tool_name}")
- The example_tools list that includes the calculate_fibonacci function, which is imported and looped over in server/app.py for registration.example_tools = [ echo_tool, get_time, random_number, calculate_fibonacci ]