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 return NewStdioMCPClientWithOptions(command, env, args)
23}
24
25// NewStdioMCPClientWithOptions creates a new stdio-based MCP client that communicates with a subprocess.
26// It launches the specified command with given arguments and sets up stdin/stdout pipes for communication.
27// Optional configuration functions can be provided to customize the transport before it starts,
28// such as setting a custom command function.
29//
30// NOTICE: NewStdioMCPClientWithOptions automatically starts the underlying transport.
31// Don't call the Start method manually.
32// This is for backward compatibility.
33func NewStdioMCPClientWithOptions(
34 command string,
35 env []string,
36 args []string,
37 opts ...transport.StdioOption,
38) (*Client, error) {
39 stdioTransport := transport.NewStdioWithOptions(command, env, args, opts...)
40
41 if err := stdioTransport.Start(context.Background()); err != nil {
42 return nil, fmt.Errorf("failed to start stdio transport: %w", err)
43 }
44
45 return NewClient(stdioTransport), nil
46}
47
48// GetStderr returns a reader for the stderr output of the subprocess.
49// This can be used to capture error messages or logs from the subprocess.
50func GetStderr(c *Client) (io.Reader, bool) {
51 t := c.GetTransport()
52
53 stdio, ok := t.(*transport.Stdio)
54 if !ok {
55 return nil, false
56 }
57
58 return stdio.Stderr(), true
59}