arcade-mcp/libs/tests/arcade_mcp_server/transports/test_stdio.py
Eric Gustin 3424ec8219
MCP Local (#563)
Versions:
* arcade-mcp\==1.0.0rc1
* arcade-mcp-server\==1.0.0rc1
* arcade-core\==2.5.0rc1
* arcade-tdk\==2.6.0rc1
* arcade-serve\==2.2.0rc1

### Summary
Adds first-class MCP support across Arcade, introduces a new MCP server
and CLI, unifies the project under the arcade-mcp name, overhauls
templates/scaffolding, and improves developer tooling, secrets
management, and examples.

### Highlights
- **MCP Server & Core**
- New MCP server with stdio and HTTP/SSE transports, session management,
resumability, and lifecycle handling.
- FastAPI-like `MCPApp` for building servers with lazy init; integrated
worker+MCP HTTP app option.
- Middleware system (logging and error handling), robust exception
hierarchy, and Pydantic-based settings.
- Async-safe managers for tools, resources, and prompts backed by
registries and locks.
- Developer-facing, transport-agnostic runtime context interfaces (logs,
tools, prompts, resources, sampling, UI, notifications).
- Conversion from Arcade ToolDefinition to MCP tool schema; OpenAI JSON
tool schema converter.
  - Parser supports `@app.tool`/`@app.tool(...)` decorators.

- **CLI**
  - New `mcp` command to run MCP servers with stdio or HTTP/SSE.
- New `secret` command to set/list/unset tool secrets (supports .env
input, preserves original casing for lookups).
- `new` command refactored; option to create a full toolkit package with
scaffolding.
  - `chat` command removed.
- `serve.py` imports updated to `arcade_serve.fastapi.telemetry`;
version retrieval now uses `arcade-mcp`.
  - `show.py` refactor to use new local catalog utilities.
- `display_tool_details` improved: adds “Default” column and handles
nested properties.

- **Configuration & Discovery**
- New `configure.py` to set up Claude Desktop, Cursor, and VS Code to
connect to local or Arcade Cloud MCP servers.
- Discovery utilities to find/install toolkits, build `ToolCatalog`s,
analyze files for tools, load kits from directories (pyproject parsing),
and build minimal toolkits.
- Better handling of provider API key resolution and evaluation suite
loading.

- **Templates & Scaffolding**
- Reorganized template structure (minimal vs full); moved
`.pre-commit-config.yaml`, `.ruff.toml`, license, Makefile, README,
tests, and tools layout to correct paths.
  - Minimal template adds `.env.example` for runtime secret injection.
- Template pyproject updated for MCP servers; includes sample server
with greeting and secret-reveal tools.
  - Authorization flow in templates simplified.

- **Repo-wide Renaming & Examples**
- Migrates references from `arcade-ai` to `arcade-mcp` across READMEs,
scripts, and package metadata.
- Examples updated (LangChain/LangGraph/AI SDK/TypeScript) and package
name changed to `arcade-mcp-sdk`.

- **Evals & Core Utilities**
- Evals now use OpenAI tooling format (`OpenAIToolList`, `to_openai`);
`tool_eval` takes `provider_api_key`.
- Core utilities: fixed `does_function_return_value` by dedenting before
parse; version bump to `2.5.0rc1` and dependency cleanup.

- **Tooling & CI**
- `setup-uv-env` action splits toolkit vs contrib dependency
installation.
- Pre-commit: excludes `libs/arcade-mcp-server/mkdocs.yml` and
`libs/tests/` from YAML and Ruff hooks; Ruff per-file ignores (e.g.,
C901 in `libs/**/*.py`, TRY400 in server docs paths).
- Makefile updates for uv env setup, quality checks, tests, builds, and
new `shell` target.
  - Added Makefile to MCP server library to streamline dev workflow.

- **Cleanup**
  - Removed `claude.json` config.
- Simplified stdio entrypoint; removed unused imports (`arcade_gmail`,
`arcade_search`).

### Breaking Changes
- **CLI**: `chat` command removed; use `mcp`, `secret`, and updated
`new`.
- **Naming**: All users should update references from `arcade-ai` to
`arcade-mcp`.
- **Templates**: File paths moved; downstream scripts referencing old
template locations may need updates.

### Getting Started
- Run an MCP server:
  - `arcade mcp --stdio --toolkits your_toolkit`
  - `arcade mcp --http --toolkits your_toolkit`
- Manage secrets:
  - `arcade secret set your_toolkit KEY=value`
  - `arcade secret list your_toolkit`
  - `arcade secret unset your_toolkit KEY`
- Configure clients:
- `arcade configure` to set up Claude Desktop, Cursor, and VS Code for
local/Arcade Cloud MCP.

---------

Co-authored-by: Sam Partee <sam@arcade-ai.com>
Co-authored-by: Shub <125150494+shubcodes@users.noreply.github.com>
2025-09-25 15:28:15 -07:00

232 lines
7.7 KiB
Python

import asyncio
import queue
from unittest.mock import MagicMock, patch
import pytest
from arcade_mcp_server.exceptions import TransportError
from arcade_mcp_server.session import ServerSession
from arcade_mcp_server.transports.stdio import (
StdioReadStream,
StdioTransport,
StdioWriteStream,
)
class TestStdioWriteStream:
"""Test StdioWriteStream functionality."""
@pytest.mark.asyncio
async def test_send_adds_newline(self):
"""Test that send adds newline to data without one."""
write_queue = queue.Queue()
stream = StdioWriteStream(write_queue)
await stream.send("test message")
# Check that newline was added
assert write_queue.get() == "test message\n"
@pytest.mark.asyncio
async def test_send_preserves_existing_newline(self):
"""Test that send doesn't add extra newline if one exists."""
write_queue = queue.Queue()
stream = StdioWriteStream(write_queue)
await stream.send("test message\n")
# Check that no extra newline was added
assert write_queue.get() == "test message\n"
class TestStdioReadStream:
"""Test StdioReadStream functionality."""
@pytest.mark.asyncio
async def test_read_stream_iteration(self):
"""Test basic iteration over read stream."""
read_queue = queue.Queue()
stream = StdioReadStream(read_queue)
# Put test data in queue
read_queue.put("line1")
read_queue.put("line2")
read_queue.put(None) # EOF marker
lines = []
async for line in stream:
lines.append(line)
assert lines == ["line1", "line2"]
@pytest.mark.asyncio
async def test_read_stream_stop(self):
"""Test that stopping the stream raises StopAsyncIteration."""
read_queue = queue.Queue()
stream = StdioReadStream(read_queue)
stream.stop()
with pytest.raises(StopAsyncIteration):
await stream.__anext__()
@pytest.mark.asyncio
async def test_read_stream_none_stops_iteration(self):
"""Test that None in queue stops iteration."""
read_queue = queue.Queue()
stream = StdioReadStream(read_queue)
read_queue.put(None)
with pytest.raises(StopAsyncIteration):
await stream.__anext__()
class TestStdioTransport:
"""Test StdioTransport functionality."""
@pytest.mark.asyncio
async def test_transport_initialization(self):
"""Test transport initializes with correct defaults."""
transport = StdioTransport()
assert transport.name == "stdio"
assert isinstance(transport.read_queue, queue.Queue)
assert isinstance(transport.write_queue, queue.Queue)
assert transport.reader_thread is None
assert transport.writer_thread is None
assert not transport._running
assert transport._sessions == {}
@pytest.mark.asyncio
async def test_transport_custom_name(self):
"""Test transport can be initialized with custom name."""
transport = StdioTransport(name="custom-stdio")
assert transport.name == "custom-stdio"
@pytest.mark.asyncio
async def test_start_creates_threads(self):
"""Test that start() creates and starts I/O threads."""
transport = StdioTransport()
with patch("threading.Thread") as mock_thread:
mock_thread_instance = MagicMock()
mock_thread.return_value = mock_thread_instance
await transport.start()
# Should create two threads (reader and writer)
assert mock_thread.call_count == 2
# Both threads should be started
assert mock_thread_instance.start.call_count == 2
assert transport._running is True
@pytest.mark.asyncio
async def test_stop_sets_running_false(self):
"""Test that stop() sets _running to False and signals threads."""
transport = StdioTransport()
transport._running = True
# Mock threads
mock_reader = MagicMock()
mock_writer = MagicMock()
mock_reader.is_alive.return_value = False
mock_writer.is_alive.return_value = False
transport.reader_thread = mock_reader
transport.writer_thread = mock_writer
await transport.stop()
assert transport._running is False
assert transport._shutdown_event.is_set()
@pytest.mark.asyncio
async def test_list_sessions_empty(self):
"""Test list_sessions returns empty list initially."""
transport = StdioTransport()
sessions = await transport.list_sessions()
assert sessions == []
@pytest.mark.asyncio
async def test_register_session(self):
"""Test session registration."""
transport = StdioTransport()
mock_session = MagicMock(spec=ServerSession)
mock_session.session_id = "test-session"
await transport.register_session(mock_session)
sessions = await transport.list_sessions()
assert sessions == ["test-session"]
assert transport._sessions["test-session"] == mock_session
@pytest.mark.asyncio
async def test_unregister_session(self):
"""Test session unregistration."""
transport = StdioTransport()
mock_session = MagicMock(spec=ServerSession)
mock_session.session_id = "test-session"
# Register then unregister
await transport.register_session(mock_session)
await transport.unregister_session("test-session")
sessions = await transport.list_sessions()
assert sessions == []
assert "test-session" not in transport._sessions
@pytest.mark.asyncio
async def test_connect_session_single_session_limit(self):
"""Test that stdio transport only allows one session."""
transport = StdioTransport()
# Mock existing session
mock_session = MagicMock(spec=ServerSession)
mock_session.session_id = "existing-session"
transport._sessions["existing-session"] = mock_session
# Try to connect another session
with pytest.raises(TransportError, match="Stdio transport only supports one session"):
async with transport.connect_session():
pass
@pytest.mark.asyncio
async def test_connect_session_creates_session(self):
"""Test that connect_session creates a proper session."""
transport = StdioTransport()
# Mock UUID generation
with patch("uuid.uuid4") as mock_uuid:
mock_uuid.return_value.return_value = "test-uuid"
mock_uuid.return_value.__str__.return_value = "test-uuid"
async with transport.connect_session() as session:
assert isinstance(session, ServerSession)
assert session.session_id == "test-uuid"
assert session.stateless is True
# Check session was registered
sessions = await transport.list_sessions()
assert "test-uuid" in sessions
# Check session was unregistered after context exit
sessions = await transport.list_sessions()
assert "test-uuid" not in sessions
@pytest.mark.asyncio
async def test_wait_for_shutdown(self):
"""Test wait_for_shutdown waits for shutdown event."""
transport = StdioTransport()
# Start a task that will set the shutdown event after a delay
async def set_shutdown():
await asyncio.sleep(0.01)
transport._shutdown_event.set()
task = asyncio.create_task(set_shutdown())
# This should complete when the event is set
await transport.wait_for_shutdown()
assert transport._shutdown_event.is_set()
await task # Clean up the task