Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func NewProtocol(options *ProtocolOptions) *Protocol {
}

// Set up default handlers
p.SetNotificationHandler("notifications/initialized", p.handleInitializedNotification)
p.SetNotificationHandler("notifications/cancelled", p.handleCancelledNotification)
p.SetNotificationHandler("$/progress", p.handleProgressNotification)

Expand Down Expand Up @@ -297,6 +298,12 @@ func (p *Protocol) handleRequest(ctx context.Context, request *transport.BaseJSO
}()
}

// Note: Not sure what this is for, yet, after perusing the MCP spec, lightly.
// Just have a placeholder here, for now, so that our server can at least receive it.
func (p *Protocol) handleInitializedNotification(notification *transport.BaseJSONRPCNotification) error {
return nil
}

func (p *Protocol) handleProgressNotification(notification *transport.BaseJSONRPCNotification) error {
var params struct {
Progress int64 `json:"progress"`
Expand Down
26 changes: 26 additions & 0 deletions transport/http/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sync"
"time"

"github.com/rvoh-emccaleb/mcp-golang/transport"
)
Expand Down Expand Up @@ -68,6 +70,30 @@ func (t *HTTPClientTransport) Send(ctx context.Context, message *transport.BaseJ
req.Header.Set(key, value)
}

if message.Type == transport.BaseMessageTypeJSONRPCNotificationType {
// For notifications, use an arbitrary short timeout
ctx, cancel := context.WithTimeout(ctx, 1*time.Millisecond)
defer cancel()
req = req.WithContext(ctx)

// Fire and forget for notifications
go func() {
resp, err := t.client.Do(req)
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
if t.errorHandler != nil {
t.errorHandler(fmt.Errorf("async notification error: %w", err))
}
}
if resp != nil {
resp.Body.Close()
}
}()

return nil
}

// For non-notifications, continue with normal synchronous request

resp, err := t.client.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
Expand Down