Skip to content

Commit 00e41fd

Browse files
committed
fixed code to correctly execute e2e test for MCP
1 parent 4b358ab commit 00e41fd

File tree

23 files changed

+1410
-342
lines changed

23 files changed

+1410
-342
lines changed

dev-tools/mcp-mock-server/server.py

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ def _capture_headers(self) -> None:
6060
if len(request_log) > 10:
6161
request_log.pop(0)
6262

63-
def do_POST(self) -> None: # pylint: disable=invalid-name
63+
def do_POST(
64+
self,
65+
) -> (
66+
None
67+
): # pylint: disable=invalid-name,too-many-locals,too-many-branches,too-many-statements
6468
"""Handle POST requests (MCP protocol endpoints)."""
6569
self._capture_headers()
6670

@@ -77,23 +81,40 @@ def do_POST(self) -> None: # pylint: disable=invalid-name
7781
request_id = 1
7882
method = "unknown"
7983

84+
# Log the RPC method in the request log
85+
if request_log:
86+
request_log[-1]["rpc_method"] = method
87+
8088
# Determine tool name based on authorization header to avoid collisions
8189
auth_header = self.headers.get("Authorization", "")
8290

8391
# Initialize tool info defaults
8492
tool_name = "mock_tool_no_auth"
8593
tool_desc = "Mock tool with no authorization"
94+
error_mode = False
8695

8796
# Match based on token content
88-
if "test-secret-token" in auth_header:
89-
tool_name = "mock_tool_file"
90-
tool_desc = "Mock tool with file-based auth"
91-
elif "my-k8s-token" in auth_header:
92-
tool_name = "mock_tool_k8s"
93-
tool_desc = "Mock tool with Kubernetes token"
94-
elif "my-client-token" in auth_header:
95-
tool_name = "mock_tool_client"
96-
tool_desc = "Mock tool with client-provided token"
97+
match True:
98+
case _ if "test-secret-token" in auth_header:
99+
tool_name = "mock_tool_file"
100+
tool_desc = "Mock tool with file-based auth"
101+
case _ if "my-k8s-token" in auth_header:
102+
tool_name = "mock_tool_k8s"
103+
tool_desc = "Mock tool with Kubernetes token"
104+
case _ if "my-client-token" in auth_header:
105+
tool_name = "mock_tool_client"
106+
tool_desc = "Mock tool with client-provided token"
107+
case _ if "error-mode" in auth_header:
108+
tool_name = "mock_tool_error"
109+
tool_desc = "Mock tool configured to return errors"
110+
error_mode = True
111+
case _:
112+
# Default case already set above
113+
pass
114+
115+
# Log the tool name in the request log
116+
if request_log:
117+
request_log[-1]["tool_name"] = tool_name
97118

98119
# Handle MCP protocol methods using match statement
99120
response: dict = {}
@@ -145,29 +166,46 @@ def do_POST(self) -> None: # pylint: disable=invalid-name
145166
tool_called = params.get("name", "unknown")
146167
arguments = params.get("arguments", {})
147168

148-
# Build result text
149-
auth_preview = (
150-
auth_header[:50] if len(auth_header) > 50 else auth_header
151-
)
152-
result_text = (
153-
f"Mock tool '{tool_called}' executed successfully "
154-
f"with arguments: {arguments}. Auth used: {auth_preview}..."
155-
)
156-
157-
# Return successful tool execution result
158-
response = {
159-
"jsonrpc": "2.0",
160-
"id": request_id,
161-
"result": {
162-
"content": [
163-
{
164-
"type": "text",
165-
"text": result_text,
166-
}
167-
],
168-
"isError": False,
169-
},
170-
}
169+
# Check if error mode is enabled
170+
if error_mode:
171+
# Return error response
172+
response = {
173+
"jsonrpc": "2.0",
174+
"id": request_id,
175+
"result": {
176+
"content": [
177+
{
178+
"type": "text",
179+
"text": (
180+
f"Error: Tool '{tool_called}' "
181+
"execution failed - simulated error."
182+
),
183+
}
184+
],
185+
"isError": True,
186+
},
187+
}
188+
else:
189+
# Build result text
190+
result_text = (
191+
f"Mock tool '{tool_called}' executed successfully "
192+
f"with arguments: {arguments}."
193+
)
194+
195+
# Return successful tool execution result
196+
response = {
197+
"jsonrpc": "2.0",
198+
"id": request_id,
199+
"result": {
200+
"content": [
201+
{
202+
"type": "text",
203+
"text": result_text,
204+
}
205+
],
206+
"isError": False,
207+
},
208+
}
171209

172210
case _:
173211
# Generic success response for other methods
@@ -194,6 +232,11 @@ def do_GET(self) -> None: # pylint: disable=invalid-name
194232
)
195233
case "/debug/requests":
196234
self._send_json_response(request_log)
235+
case "/debug/clear":
236+
# Clear the request log and last captured headers
237+
request_log.clear()
238+
last_headers.clear()
239+
self._send_json_response({"status": "cleared", "request_count": 0})
197240
case "/":
198241
self._send_help_page()
199242
case _:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Lightspeed Core Service - MCP Mock Server Test (Noop Auth)
2+
service:
3+
host: localhost
4+
port: 8080
5+
auth_enabled: false
6+
workers: 1
7+
color_log: true
8+
access_log: true
9+
llama_stack:
10+
use_as_library_client: true
11+
library_client_config_path: "dev-tools/test-configs/llama-stack-mcp-test.yaml"
12+
user_data_collection:
13+
feedback_enabled: false
14+
transcripts_enabled: false
15+
authentication:
16+
module: "noop"
17+
inference:
18+
default_model: "gpt-4o-mini"
19+
default_provider: "openai"
20+
mcp_servers:
21+
# Test 1: Static file-based authentication (HTTP)
22+
- name: "mock-file-auth"
23+
provider_id: "model-context-protocol"
24+
url: "http://localhost:9000"
25+
authorization_headers:
26+
Authorization: "/tmp/lightspeed-mcp-test-token"
27+
# Test 2: Kubernetes token forwarding (HTTP)
28+
- name: "mock-k8s-auth"
29+
provider_id: "model-context-protocol"
30+
url: "http://localhost:9000"
31+
authorization_headers:
32+
Authorization: "kubernetes"
33+
# Test 3: Client-provided token (HTTP - simplified for testing)
34+
- name: "mock-client-auth"
35+
provider_id: "model-context-protocol"
36+
url: "http://localhost:9000"
37+
authorization_headers:
38+
Authorization: "client"

docker-compose-library.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services:
66
dockerfile: dev-tools/mcp-mock-server/Dockerfile
77
container_name: mcp-mock-server
88
ports:
9-
- "3000:3000"
9+
- "9000:3000"
1010
networks:
1111
- lightspeednet
1212
healthcheck:
@@ -68,8 +68,8 @@ services:
6868
- LLAMA_STACK_LOGGING=${LLAMA_STACK_LOGGING:-}
6969
entrypoint: >
7070
/bin/bash -c "
71-
echo 'test-secret-token-123' > /tmp/lightspeed-mcp-test-token &&
72-
/opt/app-root/src/scripts/run.sh
71+
printf %s 'test-secret-token-123' > /tmp/lightspeed-mcp-test-token &&
72+
/app-root/.venv/bin/python3.12 /app-root/src/lightspeed_stack.py
7373
"
7474
healthcheck:
7575
test: ["CMD", "curl", "-f", "http://localhost:8080/liveness"]

docker-compose.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services:
66
dockerfile: dev-tools/mcp-mock-server/Dockerfile
77
container_name: mcp-mock-server
88
ports:
9-
- "3000:3000"
9+
- "9000:3000"
1010
networks:
1111
- lightspeednet
1212
healthcheck:
@@ -84,6 +84,11 @@ services:
8484
- TENANT_ID=${TENANT_ID:-}
8585
- CLIENT_ID=${CLIENT_ID:-}
8686
- CLIENT_SECRET=${CLIENT_SECRET:-}
87+
entrypoint: >
88+
/bin/bash -c "
89+
printf %s 'test-secret-token-123' > /tmp/lightspeed-mcp-test-token &&
90+
/app-root/.venv/bin/python3.12 /app-root/src/lightspeed_stack.py
91+
"
8792
depends_on:
8893
llama-stack:
8994
condition: service_healthy

0 commit comments

Comments
 (0)