calculate_fibonacci
Compute Fibonacci sequence values for SEO analysis and data processing tasks. Enter position n to generate the corresponding Fibonacci number with calculation details.
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 that computes the nth Fibonacci number using an iterative approach, handles input validation, measures computation time, and returns structured results.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:97-112 (registration)Registers calculate_fibonacci (as part of example_tools) with the MCP server by applying SAAGA decorators and using FastMCP's tool decorator.# Register regular tools with SAAGA decorators 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}")
- mcp_ahrefs/tools/example_tools.py:178-183 (registration)Includes calculate_fibonacci in the list of regular example tools that get automatically registered.example_tools = [ echo_tool, get_time, random_number, calculate_fibonacci ]