Skip to content

Commit 047d42c

Browse files
committed
Stub the nexus service
1 parent ce7de1a commit 047d42c

File tree

2 files changed

+76
-23
lines changed

2 files changed

+76
-23
lines changed

mcp_examples/common/mcp_server_nexus_service.py

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""
22
A Nexus service that presents the interface of an MCP server.
3-
It is backed by a Temporal workflow.
3+
Returns mock data for testing the transport layer.
44
"""
55

6-
import uuid
76
from dataclasses import dataclass
87

98
import nexusrpc
@@ -12,8 +11,9 @@
1211
CallToolResult,
1312
ListToolsRequest,
1413
ListToolsResult,
14+
TextContent,
15+
Tool,
1516
)
16-
from temporalio import nexus
1717

1818

1919
@dataclass
@@ -42,29 +42,72 @@ class MCPServerNexusService:
4242

4343
@nexusrpc.handler.service_handler(service=MCPServerNexusService)
4444
class MCPServerNexusServiceHandler:
45-
@nexus.workflow_run_operation
45+
@nexusrpc.handler.sync_operation
4646
async def start(
47-
self, ctx: nexus.WorkflowRunOperationContext, input: MCPServerStartInput
48-
) -> nexus.WorkflowHandle[None]:
49-
return await ctx.start_workflow(
50-
input.mcp_server_workflow_name,
51-
id=str(uuid.uuid4()),
52-
)
47+
self, ctx: nexusrpc.handler.StartOperationContext, input: MCPServerStartInput
48+
) -> None:
49+
# Mock implementation - just return None
50+
return None
5351

5452
@nexusrpc.handler.sync_operation
5553
async def call_tool(
5654
self, ctx: nexusrpc.handler.StartOperationContext, input: CallToolInput
5755
) -> CallToolResult:
58-
workflow_handle = nexus.WorkflowHandle.from_token(
59-
input.operation_token
60-
)._to_client_workflow_handle(nexus.client())
61-
return await workflow_handle.execute_update("call_tool", input.request)
56+
# Mock implementation - return a sample response
57+
if input.request.params.name == "sequentialthinking":
58+
args = input.request.params.arguments or {}
59+
thought = (
60+
args.get("thought", "Mock thought")
61+
if isinstance(args, dict)
62+
else "Mock thought"
63+
)
64+
thought_number = (
65+
args.get("thoughtNumber", 1) if isinstance(args, dict) else 1
66+
)
67+
total_thoughts = (
68+
args.get("totalThoughts", 3) if isinstance(args, dict) else 3
69+
)
70+
71+
response_text = f"Mock response: Processed thought {thought_number}/{total_thoughts}: {thought}"
72+
73+
return CallToolResult(
74+
content=[TextContent(type="text", text=response_text)]
75+
)
76+
else:
77+
return CallToolResult(
78+
content=[
79+
TextContent(
80+
type="text",
81+
text=f"Mock response: Tool {input.request.params.name} called",
82+
)
83+
]
84+
)
6285

6386
@nexusrpc.handler.sync_operation
6487
async def list_tools(
6588
self, ctx: nexusrpc.handler.StartOperationContext, input: ListToolsInput
6689
) -> ListToolsResult:
67-
workflow_handle = nexus.WorkflowHandle.from_token(
68-
input.operation_token
69-
)._to_client_workflow_handle(nexus.client())
70-
return await workflow_handle.query("list_tools", input.request)
90+
# Mock implementation - return sequential thinking tool
91+
return ListToolsResult(
92+
tools=[
93+
Tool(
94+
name="sequentialthinking",
95+
description="A tool for sequential thinking and problem-solving",
96+
inputSchema={
97+
"type": "object",
98+
"properties": {
99+
"thought": {"type": "string"},
100+
"thoughtNumber": {"type": "integer"},
101+
"totalThoughts": {"type": "integer"},
102+
"nextThoughtNeeded": {"type": "boolean"},
103+
},
104+
"required": [
105+
"thought",
106+
"thoughtNumber",
107+
"totalThoughts",
108+
"nextThoughtNeeded",
109+
],
110+
},
111+
)
112+
]
113+
)

mcp_examples/nexus_transport/app.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
from mcp import ClientSession, StdioServerParameters
88
from mcp.client.stdio import stdio_client
99
from mcp.types import TextContent
10-
from temporalio import workflow
11-
from temporalio.client import Client
12-
from temporalio.worker import UnsandboxedWorkflowRunner, Worker
13-
1410
from mcp_examples.common.mcp_sdk_nexus_transport import NexusTransport
1511
from mcp_examples.common.mcp_server_nexus_service import (
1612
MCPServerNexusService,
1713
MCPServerNexusServiceHandler,
1814
)
15+
from temporalio import workflow
16+
from temporalio.client import Client
17+
from temporalio.worker import UnsandboxedWorkflowRunner, Worker
1918

2019

2120
async def create_client_session_and_call_tool_using_standard_transport():
@@ -32,11 +31,16 @@ async def create_client_session_and_call_tool_using_standard_transport():
3231
async def create_client_session_and_call_tool_using_nexus_transport(
3332
nexus_client: workflow.NexusClient[MCPServerNexusService],
3433
):
34+
print("Creating NexusTransport...")
3535
transport = NexusTransport(nexus_client, "mcp-sequential-thinking-nexus-endpoint")
3636

37+
print("Connecting transport...")
3738
async with transport.connect() as (read_stream, write_stream):
39+
print("Creating ClientSession...")
3840
async with ClientSession(read_stream, write_stream) as session:
41+
print("Initializing session...")
3942
await session.initialize()
43+
print("Calling tool...")
4044
await _call_tool(session)
4145

4246

@@ -52,13 +56,17 @@ def __init__(self):
5256

5357
@workflow.run
5458
async def run(self):
59+
print("CallToolWorkflow.run() started")
5560
await create_client_session_and_call_tool_using_nexus_transport(
5661
self.nexus_client
5762
)
63+
print("CallToolWorkflow.run() completed")
5864

5965

6066
async def main():
67+
print("Connecting to Temporal...")
6168
client = await Client.connect("localhost:7233")
69+
print("Creating worker...")
6270
async with Worker(
6371
client,
6472
task_queue="mcp-sequential-thinking-task-queue",
@@ -68,11 +76,13 @@ async def main():
6876
nexus_service_handlers=[MCPServerNexusServiceHandler()],
6977
workflow_runner=UnsandboxedWorkflowRunner(),
7078
) as worker:
71-
await client.execute_workflow(
79+
print("Executing workflow...")
80+
result = await client.execute_workflow(
7281
CallToolWorkflow.run,
7382
id=str(uuid.uuid4()),
7483
task_queue=worker.task_queue,
7584
)
85+
print(f"Workflow completed: {result}")
7686

7787

7888
async def _call_tool(session: ClientSession):

0 commit comments

Comments
 (0)