arcade-mcp/arcade/tests/mcp/test_message_processor.py
Sam Partee 9bc1cd4a12
Support for MCP stdio transport (#368)
MCP stdio Implementation:

The PR adds support for standard input/output (stdio) as a transport
mechanism for the Message Control Protocol. This is a replacement to the
SSE (Server-Sent Events) transport that was worked on in PR #359 but
will not be merged as it's not deprecated.

This will allow developers to use Arcade tools (written by the dev or
Arcade) in Claude, Cursor, windsurf, etc.

The engine Gateway already supports adding HTTPS streamable (replacement
for SSE) MCP servers as tool servers, and will soon support full gateway
capability in the client API as well.

To use any existing Toolkit just 

## Examples

### Quickstart setup with existing toolkits

```bash
pip install arcade-ai
pip install <name of toolkit> # ex. arcade-google
arcade serve --mcp
```

### Run with Claude

Just add the following to the Claude config

```json
{
  "mcpServers": {
    "arcade": {
      "command": "bash",
      "args": ["-c", "export ARCADE_API_KEY=arc_xxxx && /path/to/python /path/to/arcade serve --mcp"]
    }
  }
}
```

### Customizing the Tool Server

Developers can customize their served tools and server furthermore by
importing the worker sdk

```python

import arcade_google  # pip install arcade_google
import arcade_search  # pip install arcade_search

from arcade.core.catalog import ToolCatalog
from arcade.worker.mcp.stdio import StdioServer

# 2. Create and populate the tool catalog
catalog = ToolCatalog()
catalog.add_module(arcade_google)  # Registers all tools in the package
catalog.add_module(arcade_search)


# 3. Main entrypoint
async def main():
    # Create the worker with the tool catalog
    worker = StdioServer(catalog)

    # Run the worker
    await worker.run()


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())
    
 ```
 
 Then to run with claude, just run this python file instead of the prebuilt server used in ``arcade serve --mcp``
2025-05-02 06:27:43 -07:00

57 lines
1.9 KiB
Python

import asyncio
import pytest
from arcade.worker.mcp.message_processor import MCPMessageProcessor, create_message_processor
from arcade.worker.mcp.types import InitializeRequest, PingRequest
@pytest.mark.asyncio
async def test_message_processor_parses_initialize_json():
"""Ensure JSON initialize strings are converted into InitializeRequest objects."""
json_init = '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}\n'
processor = MCPMessageProcessor()
result = await processor.process_request(json_init)
assert isinstance(result, InitializeRequest)
assert result.id == 1
assert result.method == "initialize"
@pytest.mark.asyncio
async def test_message_processor_passes_notifications_unchanged():
"""Unknown notifications should be passed through as parsed dictionaries without errors."""
json_notification = '{"jsonrpc":"2.0","id":null,"method":"notifications/custom","params":{}}\n'
processor = MCPMessageProcessor()
result = await processor.process_request(json_notification)
# The MCPMessageProcessor keeps unknown notifications as simple dicts
assert isinstance(result, dict)
assert result["method"] == "notifications/custom"
@pytest.mark.asyncio
async def test_message_processor_middleware_execution_order(monkeypatch):
"""Middleware (sync + async) should be executed in the order they were added."""
order: list[str] = []
def mw_sync(msg, direction): # type: ignore[return-value]
order.append("sync")
return msg
async def mw_async(msg, direction): # type: ignore[return-value]
await asyncio.sleep(0) # ensure it is truly async
order.append("async")
return msg
processor = create_message_processor(mw_sync, mw_async)
# Use a pre-parsed PingRequest instance so we don't test parsing again here
ping = PingRequest(id=42)
_ = await processor.process_request(ping)
assert order == ["sync", "async"]