"""Calculator service handler implementation with MCP integration."""
import uuid
import logging
import nexusrpc
from temporalio import nexus
from nexusmcp import MCPServiceHandler
from nexus_mcp_calculator.service import (
CalculatorService,
CalculateRequest,
CalculateResponse,
AddRequest,
SubtractRequest,
MultiplyRequest,
DivideRequest,
PowerRequest,
SumListRequest,
BasicOperationResponse,
)
from nexus_mcp_calculator.workflows import (
CalculateWorkflow,
AddWorkflow,
SubtractWorkflow,
MultiplyWorkflow,
DivideWorkflow,
PowerWorkflow,
SumListWorkflow,
)
# Set up logging
logger = logging.getLogger(__name__)
# Create the MCP service handler registry
mcp_service_handler = MCPServiceHandler()
@mcp_service_handler.register
@nexusrpc.handler.service_handler(service=CalculatorService)
class CalculatorHandler:
"""Nexus service handler for calculator operations.
This handler now uses workflow_run_operations that start workflows,
which execute activities for the actual calculations. This provides
full visibility in the Temporal UI for both workflow and activity executions.
"""
@nexus.workflow_run_operation
async def calculate(
self,
ctx: nexus.WorkflowRunOperationContext,
input: CalculateRequest
) -> nexus.WorkflowHandle[CalculateResponse]:
"""Start a workflow to evaluate a mathematical expression.
This operation starts a CalculateWorkflow which executes a CalculateActivity
for the actual calculation. Both the workflow and activity are visible
in the Temporal UI.
"""
logger.info(f"๐งฎ Calculator.calculate operation starting workflow for: '{input.expression}'")
return await ctx.start_workflow(
CalculateWorkflow.run,
input,
id=f"calculate-{uuid.uuid4()}",
)
@nexus.workflow_run_operation
async def add(
self,
ctx: nexus.WorkflowRunOperationContext,
input: AddRequest
) -> nexus.WorkflowHandle[BasicOperationResponse]:
"""Start a workflow to add two numbers together."""
logger.info(f"โ Calculator.add operation starting workflow for: {input.a} + {input.b}")
return await ctx.start_workflow(
AddWorkflow.run,
input,
id=f"add-{uuid.uuid4()}",
)
@nexus.workflow_run_operation
async def subtract(
self,
ctx: nexus.WorkflowRunOperationContext,
input: SubtractRequest
) -> nexus.WorkflowHandle[BasicOperationResponse]:
"""Start a workflow to subtract two numbers."""
logger.info(f"โ Calculator.subtract operation starting workflow for: {input.a} - {input.b}")
return await ctx.start_workflow(
SubtractWorkflow.run,
input,
id=f"subtract-{uuid.uuid4()}",
)
@nexus.workflow_run_operation
async def multiply(
self,
ctx: nexus.WorkflowRunOperationContext,
input: MultiplyRequest
) -> nexus.WorkflowHandle[BasicOperationResponse]:
"""Start a workflow to multiply two numbers."""
logger.info(f"โ๏ธ Calculator.multiply operation starting workflow for: {input.a} * {input.b}")
return await ctx.start_workflow(
MultiplyWorkflow.run,
input,
id=f"multiply-{uuid.uuid4()}",
)
@nexus.workflow_run_operation
async def divide(
self,
ctx: nexus.WorkflowRunOperationContext,
input: DivideRequest
) -> nexus.WorkflowHandle[BasicOperationResponse]:
"""Start a workflow to divide two numbers."""
logger.info(f"โ Calculator.divide operation starting workflow for: {input.a} / {input.b}")
return await ctx.start_workflow(
DivideWorkflow.run,
input,
id=f"divide-{uuid.uuid4()}",
)
@nexus.workflow_run_operation
async def power(
self,
ctx: nexus.WorkflowRunOperationContext,
input: PowerRequest
) -> nexus.WorkflowHandle[BasicOperationResponse]:
"""Start a workflow to raise a number to a power."""
logger.info(f"๐ข Calculator.power operation starting workflow for: {input.base} ^ {input.exponent}")
return await ctx.start_workflow(
PowerWorkflow.run,
input,
id=f"power-{uuid.uuid4()}",
)
@nexus.workflow_run_operation
async def sum_list(
self,
ctx: nexus.WorkflowRunOperationContext,
input: SumListRequest
) -> nexus.WorkflowHandle[BasicOperationResponse]:
"""Start a workflow to sum a list of numbers."""
logger.info(f"๐ Calculator.sum_list operation starting workflow for {len(input.numbers)} numbers")
return await ctx.start_workflow(
SumListWorkflow.run,
input,
id=f"sum_list-{uuid.uuid4()}",
)