Skip to content

Commit 1e81056

Browse files
authored
feat(tests): enable MCP tests in server mode (#4146)
We would like to run all OpenAI compatibility tests using only the openai-client library. This is most friendly for contributors since they can run tests without needing to update the client-sdks (which is getting easier but still a long pole.) This is the first step in enabling that -- no using "library client" for any of the Responses tests. This seems like a reasonable trade-off since the usage of an embeddeble library client for Responses (or any OpenAI-compatible) behavior seems to be not very common. To do this, we needed to enable MCP tests (which only worked in library client mode) for server mode.
1 parent 9eb8143 commit 1e81056

File tree

29 files changed

+13388
-127
lines changed

29 files changed

+13388
-127
lines changed

scripts/integration-tests.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ if [[ "$COLLECT_ONLY" == false ]]; then
162162
export LLAMA_STACK_TEST_STACK_CONFIG_TYPE="library_client"
163163
echo "Setting stack config type: library_client"
164164
fi
165+
166+
# Set MCP host for in-process MCP server tests
167+
# - For library client and server mode: localhost (both on same host)
168+
# - For docker mode: host.docker.internal (container needs to reach host)
169+
if [[ "$STACK_CONFIG" == docker:* ]]; then
170+
export LLAMA_STACK_TEST_MCP_HOST="host.docker.internal"
171+
echo "Setting MCP host: host.docker.internal (docker mode)"
172+
else
173+
export LLAMA_STACK_TEST_MCP_HOST="localhost"
174+
echo "Setting MCP host: localhost (library/server mode)"
175+
fi
165176
fi
166177

167178
SETUP_ENV=$(PYTHONPATH=$THIS_DIR/.. python "$THIS_DIR/get_setup_env.py" --suite "$TEST_SUITE" --setup "$TEST_SETUP" --format bash)
@@ -338,6 +349,7 @@ if [[ "$STACK_CONFIG" == *"docker:"* && "$COLLECT_ONLY" == false ]]; then
338349
DOCKER_ENV_VARS=""
339350
DOCKER_ENV_VARS="$DOCKER_ENV_VARS -e LLAMA_STACK_TEST_INFERENCE_MODE=$INFERENCE_MODE"
340351
DOCKER_ENV_VARS="$DOCKER_ENV_VARS -e LLAMA_STACK_TEST_STACK_CONFIG_TYPE=server"
352+
DOCKER_ENV_VARS="$DOCKER_ENV_VARS -e LLAMA_STACK_TEST_MCP_HOST=${LLAMA_STACK_TEST_MCP_HOST:-host.docker.internal}"
341353
# Disabled: https://github.com/llamastack/llama-stack/issues/4089
342354
#DOCKER_ENV_VARS="$DOCKER_ENV_VARS -e OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:${COLLECTOR_PORT}"
343355
DOCKER_ENV_VARS="$DOCKER_ENV_VARS -e OTEL_METRIC_EXPORT_INTERVAL=200"
@@ -371,8 +383,11 @@ if [[ "$STACK_CONFIG" == *"docker:"* && "$COLLECT_ONLY" == false ]]; then
371383
# Use regular port mapping instead
372384
NETWORK_MODE=""
373385
PORT_MAPPINGS=""
386+
ADD_HOST_FLAG=""
374387
if [[ "$(uname)" != "Darwin" ]] && [[ "$(uname)" != *"MINGW"* ]]; then
375388
NETWORK_MODE="--network host"
389+
# On Linux with host network, also add host.docker.internal mapping for consistency
390+
ADD_HOST_FLAG="--add-host=host.docker.internal:host-gateway"
376391
else
377392
# On non-Linux (macOS, Windows), need explicit port mappings for both app and telemetry
378393
PORT_MAPPINGS="-p $LLAMA_STACK_PORT:$LLAMA_STACK_PORT -p $COLLECTOR_PORT:$COLLECTOR_PORT"
@@ -381,6 +396,7 @@ if [[ "$STACK_CONFIG" == *"docker:"* && "$COLLECT_ONLY" == false ]]; then
381396

382397
docker run -d $NETWORK_MODE --name "$container_name" \
383398
$PORT_MAPPINGS \
399+
$ADD_HOST_FLAG \
384400
$DOCKER_ENV_VARS \
385401
"$IMAGE_NAME" \
386402
--port $LLAMA_STACK_PORT

tests/common/mcp.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,14 @@ def run_server():
244244
timeout = 2
245245
start_time = time.time()
246246

247-
server_url = f"http://localhost:{port}/sse"
248-
logger.debug(f"Waiting for MCP server thread to start on port {port}")
247+
# Determine the appropriate host for the server URL based on test environment
248+
# - For library client and server mode: use localhost (both on same host)
249+
# - For docker mode: use host.docker.internal (container needs to reach host)
250+
import os
251+
252+
mcp_host = os.environ.get("LLAMA_STACK_TEST_MCP_HOST", "localhost")
253+
server_url = f"http://{mcp_host}:{port}/sse"
254+
logger.debug(f"Waiting for MCP server thread to start on port {port} (accessible via {mcp_host})")
249255

250256
while time.time() - start_time < timeout:
251257
if server_thread.is_alive():
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the terms described in the LICENSE file in
5+
# the root directory of this source tree.
6+
7+
import pytest
8+
9+
from llama_stack.core.library_client import LlamaStackAsLibraryClient
10+
11+
12+
@pytest.fixture
13+
def responses_client(compat_client):
14+
"""Provide a client for responses tests, skipping library client mode."""
15+
if isinstance(compat_client, LlamaStackAsLibraryClient):
16+
pytest.skip("Responses API tests are not supported in library client mode")
17+
return compat_client

0 commit comments

Comments
 (0)