1package transport
 2
 3import (
 4	"context"
 5	"encoding/json"
 6
 7	"github.com/mark3labs/mcp-go/mcp"
 8)
 9
10// HTTPHeaderFunc is a function that extracts header entries from the given context
11// and returns them as key-value pairs. This is typically used to add context values
12// as HTTP headers in outgoing requests.
13type HTTPHeaderFunc func(context.Context) map[string]string
14
15// Interface for the transport layer.
16type Interface interface {
17	// Start the connection. Start should only be called once.
18	Start(ctx context.Context) error
19
20	// SendRequest sends a json RPC request and returns the response synchronously.
21	SendRequest(ctx context.Context, request JSONRPCRequest) (*JSONRPCResponse, error)
22
23	// SendNotification sends a json RPC Notification to the server.
24	SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error
25
26	// SetNotificationHandler sets the handler for notifications.
27	// Any notification before the handler is set will be discarded.
28	SetNotificationHandler(handler func(notification mcp.JSONRPCNotification))
29
30	// Close the connection.
31	Close() error
32}
33
34type JSONRPCRequest struct {
35	JSONRPC string        `json:"jsonrpc"`
36	ID      mcp.RequestId `json:"id"`
37	Method  string        `json:"method"`
38	Params  any           `json:"params,omitempty"`
39}
40
41type JSONRPCResponse struct {
42	JSONRPC string          `json:"jsonrpc"`
43	ID      mcp.RequestId   `json:"id"`
44	Result  json.RawMessage `json:"result"`
45	Error   *struct {
46		Code    int             `json:"code"`
47		Message string          `json:"message"`
48		Data    json.RawMessage `json:"data"`
49	} `json:"error"`
50}