Skip to content

Commit

Permalink
♻️ Simplify transport options by making command the default
Browse files Browse the repository at this point in the history
  • Loading branch information
wesen committed Jan 21, 2025
1 parent 325df61 commit f7da7d4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 47 deletions.
109 changes: 72 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,49 +132,81 @@ The server implements the MCP specification methods:
- `tools/list` - List available tools
- `tools/call` - Execute a tool

## Usage
## Running

### Running the Server
### Building the Client and Server

Build and run the example stdio server:
First, build both the client and server:

```bash
go build -o stdio-server cmd/stdio-server/main.go
./stdio-server
# Build the client
go build -o mcp-client ./cmd/mcp-client/main.go

# Build the server
go build -o mcp-server ./cmd/mcp-server/main.go
```

### Client Connection

The server accepts JSON-RPC messages on stdin and writes responses to stdout. Example initialization:

```json
{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {
"prompts": {
"listChanged": true
},
"resources": {
"subscribe": true,
"listChanged": true
},
"tools": {
"listChanged": true
}
},
"clientInfo": {
"name": "example-client",
"version": "1.0.0"
}
}
}
### Basic Usage

The client supports two transport types:
- `command` (default): Launch and communicate with an MCP server process
- `sse`: Server-Sent Events over HTTP for web applications

#### Using Command Transport (Default)

The command transport is the default way to interact with an MCP server:

```bash
# List available prompts
./mcp-client prompts list

# List available tools
./mcp-client tools list

# Execute a prompt with arguments
./mcp-client prompts execute hello --args '{"name":"World"}'

# Call a tool with arguments
./mcp-client tools call echo --args '{"message":"Hello, MCP!"}'
```

You can customize the server command and arguments if needed:

```bash
# Use a different server binary
./mcp-client --command ./custom-server prompts list

# Pass custom arguments to the server
./mcp-client --args start,--debug,--port,8001 prompts list
```

#### Using SSE Transport

For web-based applications, use the SSE transport:

```bash
# Start the server with SSE transport
./mcp-server start --transport sse --port 8000

# In another terminal, connect using the client
./mcp-client --transport sse --server http://localhost:8000 prompts list
```

### Debug Mode

Add the `--debug` flag to enable detailed logging:

```bash
./mcp-client --debug prompts list
```

## Development
### Version Information

Check the version of the client:

```bash
./mcp-client version
```

### Project Structure

Expand All @@ -185,11 +217,14 @@ The server accepts JSON-RPC messages on stdin and writes responses to stdout. Ex
- `tools/` - Tool registry and handlers
- `server/` - Server implementation
- `doc/` - Documentation
- `cmd/stdio-server/` - Example stdio server implementation
- `cmd/`
- `mcp-client/` - MCP client implementation
- `mcp-server/` - MCP server implementation

### Dependencies

- `github.com/rs/zerolog` - Structured logging
- `github.com/spf13/cobra` - Command-line interface
- Standard Go libraries

## Contributing
Expand All @@ -198,4 +233,4 @@ Contributions are welcome! Please feel free to submit a Pull Request. For major

## License

[Insert your chosen license here]
[Insert your chosen license here]
15 changes: 5 additions & 10 deletions cmd/mcp-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ Supports both stdio and SSE transports for client-server communication.`,

// Add persistent flags
rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "Enable debug logging")
rootCmd.PersistentFlags().StringVarP(&transport, "transport", "t", "stdio", "Transport type (stdio, sse, or command)")
rootCmd.PersistentFlags().StringVarP(&transport, "transport", "t", "command", "Transport type (command or sse)")
rootCmd.PersistentFlags().StringVarP(&serverURL, "server", "s", "http://localhost:8000", "Server URL for SSE transport")
rootCmd.PersistentFlags().StringVarP(&command, "command", "c", "", "Command to run for command transport")
rootCmd.PersistentFlags().StringSliceVarP(&cmdArgs, "args", "a", []string{}, "Command arguments for command transport")
rootCmd.PersistentFlags().StringVarP(&command, "command", "c", "mcp-server", "Command to run for command transport")
rootCmd.PersistentFlags().StringSliceVarP(&cmdArgs, "args", "a", []string{"start", "--transport", "stdio"}, "Command arguments for command transport")

// Prompts command group
promptsCmd := &cobra.Command{
Expand Down Expand Up @@ -307,18 +307,13 @@ func createClient() (*client.Client, error) {
var err error

switch transport {
case "stdio":
t = client.NewStdioTransport()
case "sse":
t = client.NewSSETransport(serverURL)
case "command":
if command == "" {
return nil, fmt.Errorf("command is required for command transport")
}
t, err = client.NewCommandStdioTransport(command, cmdArgs...)
if err != nil {
return nil, fmt.Errorf("failed to create command transport: %w", err)
}
case "sse":
t = client.NewSSETransport(serverURL)
default:
return nil, fmt.Errorf("invalid transport type: %s", transport)
}
Expand Down

0 comments on commit f7da7d4

Please sign in to comment.