Skip to content

Latest commit

 

History

History
65 lines (47 loc) · 2.11 KB

stdio.md

File metadata and controls

65 lines (47 loc) · 2.11 KB

STDIO transport

The STDIO transport communicates via standard input/output streams, ideal for command-line tools and MCP client integrations.

use Mcp\Server\Transport\StdioTransport;

$transport = new StdioTransport(
    input: STDIN,           // Input stream (default: STDIN)
    output: STDOUT,         // Output stream (default: STDOUT)
    logger: $logger         // Optional PSR-3 logger
);

Parameters

  • input (optional): Input stream resource. Defaults to STDIN.
  • output (optional): Output stream resource. Defaults to STDOUT.
  • logger (optional): LoggerInterface - PSR-3 logger for debugging. Defaults to NullLogger.
  • runnerControl (optional): RunnerControlInterface - controls the read loop. Defaults to a RunnerControl, which runs until the input stream closes.
  • maxLineBytes (optional): Maximum accepted line length in bytes, including the newline. Defaults to 4 MiB (StdioTransport::DEFAULT_MAX_LINE_BYTES). The transport discards a longer line and logs a warning. It writes nothing back to the client. A value below 1 throws InvalidArgumentException.

The STDIO transport serves both eras. The client's first request decides which one the connection uses, see Serving both eras.

!!! warning When using STDIO transport, never write to STDOUT in your handlers as it's reserved for JSON-RPC communication. Use STDERR for debugging instead.

Example Server Script

<?php

require __DIR__.'/vendor/autoload.php';

use Mcp\Server\Server;
use Mcp\Server\Transport\StdioTransport;

$server = Server::builder()
    ->setServerInfo('STDIO Calculator', '1.0.0')
    ->addTool(function(int $a, int $b): int { return $a + $b; }, 'add_numbers')
    ->build();

$transport = new StdioTransport();

$status = $server->run($transport);

exit($status); // listen() returns 0 when the input stream closes

Client Configuration

For MCP clients like Claude Desktop:

{
    "mcpServers": {
        "my-php-server": {
            "command": "php",
            "args": ["/absolute/path/to/server.php"]
        }
    }
}