Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrisbattarbee committed Jan 22, 2025
1 parent 059a941 commit be9a884
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 4 deletions.
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Docs at [https://mcpgolang.com](https://mcpgolang.com)

## Highlights
- 🛡️**Type safety** - Define your tool arguments as native go structs, have mcp-golang handle the rest. Automatic schema generation, deserialization, error handling etc.
- 🚛 **Custom transports** - Use the built-in transports or write your own.
- 🚛 **Custom transports** - Use the built-in transports (stdio for full feature support, HTTP for stateless communication) or write your own.
-**Low boilerplate** - mcp-golang generates all the MCP endpoints for you apart from your tools, prompts and resources.
- 🧩 **Modular** - The library is split into three components: transport, protocol and server/client. Use them all or take what you need.
- 🔄 **Bi-directional** - Full support for both server and client implementations.
- 🔄 **Bi-directional** - Full support for both server and client implementations through stdio transport.

## Example Usage

Expand Down Expand Up @@ -92,6 +92,25 @@ func main() {
}
```

### HTTP Server Example

You can also create an HTTP-based server using either the standard HTTP transport or Gin framework:

```go
// Standard HTTP
transport := http.NewHTTPTransport("/mcp")
transport.WithAddr(":8080")
server := mcp_golang.NewServer(transport)

// Or with Gin framework
transport := http.NewGinTransport()
router := gin.Default()
router.POST("/mcp", transport.Handler())
server := mcp_golang.NewServer(transport)
```

Note: HTTP transports are stateless and don't support bidirectional features like notifications. Use stdio transport if you need those features.

### Client Example

Checkout the [examples/client](./examples/client) directory for a more complete example.
Expand Down Expand Up @@ -209,7 +228,9 @@ Open a PR to add your own projects!
- [x] Pagination

### Transports
- [x] Stdio
- [x] Stdio - Full support for all features including bidirectional communication
- [x] HTTP - Stateless transport for simple request-response scenarios (no notifications support)
- [x] Gin - HTTP transport with Gin framework integration (stateless, no notifications support)
- [x] SSE
- [x] Custom transport support
- [ ] HTTPS with custom auth support - in progress. Not currently part of the spec but we'll be adding experimental support for it.
Expand Down
58 changes: 57 additions & 1 deletion docs/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,60 @@ if err != nil {

## Complete Example

For a complete working example, check out our [example client implementation](https://github.com/metoro-io/mcp-golang/tree/main/examples/client).
For a complete working example, check out our [example client implementation](https://github.com/metoro-io/mcp-golang/tree/main/examples/client).

## Transport Options

The MCP client supports multiple transport options:

### Standard I/O Transport

For command-line tools that communicate through stdin/stdout:

```go
transport := stdio.NewStdioClientTransport()
client := mcp.NewClient(transport)
```

This transport supports all MCP features including bidirectional communication and notifications.

### HTTP Transport

For web-based tools that communicate over HTTP/HTTPS:

```go
transport := http.NewHTTPClientTransport("/mcp")
transport.WithBaseURL("http://localhost:8080")
client := mcp.NewClient(transport)
```

Note that the HTTP transport is stateless and does not support bidirectional features like notifications. Each request-response cycle is independent, making it suitable for simple tool invocations but not for scenarios requiring real-time updates or persistent connections.

## Context Support

All client operations now support context propagation:

```go
ctx := context.Background()
// With timeout
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

// Call tool with context
response, err := client.CallTool(ctx, "tool-name", args)
if err != nil {
// Handle error
}

// List tools with context
tools, err := client.ListTools(ctx)
if err != nil {
// Handle error
}
```

The context allows you to:
- Set timeouts for operations
- Cancel long-running operations
- Pass request-scoped values
- Implement tracing and monitoring
96 changes: 96 additions & 0 deletions docs/quickstart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,102 @@ Create a file in ~/Library/Application Support/Claude/claude_desktop_config.json
}
```

## HTTP Server Example

You can also create an HTTP-based MCP server. Note that HTTP transport is stateless and doesn't support bidirectional features like notifications - use stdio transport if you need those features.

```go
package main

import (
"context"
"log"
"github.com/metoro-io/mcp-golang"
"github.com/metoro-io/mcp-golang/transport/http"
)

func main() {
// Create an HTTP transport
transport := http.NewHTTPTransport("/mcp")
transport.WithAddr(":8080")

// Create server with the HTTP transport
server := mcp.NewServer(transport)

// Register your tools
server.RegisterTool("hello", &HelloTool{})

// Start the server
if err := server.Serve(); err != nil {
log.Fatal(err)
}
}
```

Or using the Gin framework:

```go
package main

import (
"github.com/gin-gonic/gin"
"github.com/metoro-io/mcp-golang"
"github.com/metoro-io/mcp-golang/transport/http"
)

func main() {
// Create a Gin transport
transport := http.NewGinTransport()

// Create server with the Gin transport
server := mcp.NewServer(transport)

// Register your tools
server.RegisterTool("hello", &HelloTool{})

// Set up Gin router
router := gin.Default()
router.POST("/mcp", transport.Handler())

// Start the server
router.Run(":8080")
}
```

## HTTP Client Example

To connect to an HTTP-based MCP server:

```go
package main

import (
"context"
"log"
"github.com/metoro-io/mcp-golang"
"github.com/metoro-io/mcp-golang/transport/http"
)

func main() {
// Create an HTTP client transport
transport := http.NewHTTPClientTransport("/mcp")
transport.WithBaseURL("http://localhost:8080")

// Create client with the HTTP transport
client := mcp.NewClient(transport)

// Use the client with context
ctx := context.Background()
response, err := client.CallTool(ctx, "hello", map[string]interface{}{
"name": "World",
})
if err != nil {
log.Fatal(err)
}
log.Printf("Response: %v", response)
}
```

## Next Steps

- If you're interested in contributing to mcp-golang, check out [Development Guide](/development) for more detailed information
Expand Down
48 changes: 48 additions & 0 deletions docs/tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,52 @@ server: {"id":10,"jsonrpc":"2.0","result":{"content":[{"text":"Hello, claude!","
* **Optional fields** All fields are optional by default. Just don't use the `jsonschema:"required"` tag.
* **Description** Use the `jsonschema:"description"` tag to add a description to the argument.

## HTTP Transport

The MCP SDK now supports HTTP transport for both client and server implementations. This allows you to build MCP tools that communicate over HTTP/HTTPS endpoints.

**Note:** The HTTP transport implementations are stateless, which means they don't support bidirectional communication features like notifications. If you need to send notifications or maintain a persistent connection between client and server, use the stdio transport instead.

### HTTP Server

There are two server implementations available:

1. Standard HTTP Server:
```go
transport := http.NewHTTPTransport("/mcp")
transport.WithAddr(":8080") // Optional, defaults to :8080
```

2. Gin Framework Server:
```go
transport := http.NewGinTransport()
router := gin.Default()
router.POST("/mcp", transport.Handler())
```

### HTTP Client

The HTTP client transport allows you to connect to MCP servers over HTTP:

```go
transport := http.NewHTTPClientTransport("/mcp")
transport.WithBaseURL("http://localhost:8080")
```

### Context Support

All transport implementations now support context propagation. This allows you to pass request-scoped data and handle timeouts/cancellation:

```go
transport.SetMessageHandler(func(ctx context.Context, msg *transport.BaseJsonRpcMessage) {
// Access context values or handle cancellation
if deadline, ok := ctx.Deadline(); ok {
// Handle deadline
}
// Process message
})
```

The context is propagated through all MCP operations, making it easier to implement timeouts, tracing, and other cross-cutting concerns.


0 comments on commit be9a884

Please sign in to comment.