stdio.go

 1package client
 2
 3import (
 4	"context"
 5	"fmt"
 6	"io"
 7
 8	"github.com/mark3labs/mcp-go/client/transport"
 9)
10
11// NewStdioMCPClient creates a new stdio-based MCP client that communicates with a subprocess.
12// It launches the specified command with given arguments and sets up stdin/stdout pipes for communication.
13// Returns an error if the subprocess cannot be started or the pipes cannot be created.
14//
15// NOTICE: NewStdioMCPClient will start the connection automatically. Don't call the Start method manually.
16// This is for backward compatibility.
17func NewStdioMCPClient(
18	command string,
19	env []string,
20	args ...string,
21) (*Client, error) {
22
23	stdioTransport := transport.NewStdio(command, env, args...)
24	err := stdioTransport.Start(context.Background())
25	if err != nil {
26		return nil, fmt.Errorf("failed to start stdio transport: %w", err)
27	}
28
29	return NewClient(stdioTransport), nil
30}
31
32// GetStderr returns a reader for the stderr output of the subprocess.
33// This can be used to capture error messages or logs from the subprocess.
34func GetStderr(c *Client) (io.Reader, bool) {
35	t := c.GetTransport()
36
37	stdio, ok := t.(*transport.Stdio)
38	if !ok {
39		return nil, false
40	}
41
42	return stdio.Stderr(), true
43}