sse

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

/* Package mcp implements Server-Sent Events (SSE) transport for JSON-RPC communication.

SSE Transport Overview: This implementation provides a bidirectional communication channel between client and server: - Server to Client: Uses Server-Sent Events (SSE) for real-time message streaming - Client to Server: Uses HTTP POST requests for sending messages

Key Features: 1. Bidirectional Communication:

  • SSE for server-to-client streaming (one-way, real-time updates)
  • HTTP POST endpoints for client-to-server messages

2. Session Management:

  • Unique session IDs for each connection
  • Proper connection lifecycle management
  • Automatic cleanup on connection close

3. Message Handling:

  • JSON-RPC message format support
  • Automatic message type detection (request vs response)
  • Built-in error handling and reporting
  • Message size limits for security

4. Security Features:

  • Content-type validation
  • Message size limits (4MB default)
  • Error handling for malformed messages

Usage Example:

// Create a new SSE transport
transport, err := NewSSETransport("/messages", responseWriter)
if err != nil {
    log.Fatal(err)
}

// Set up message handling
transport.SetMessageHandler(func(msg JSONRPCMessage) {
    // Handle incoming messages
})

// Start the SSE connection
if err := transport.Start(context.Background()); err != nil {
    log.Fatal(err)
}

// Send a message
msg := JSONRPCResponse{
    Jsonrpc: "2.0",
    Result:  Result{...},
    Id:      1,
}
if err := transport.Send(msg); err != nil {
    log.Fatal(err)
}

*/

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL