|
| 1 | +package transport |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +// Http implements the Transport interface by communicating with a MCP server over HTTP using JSON-RPC. |
| 17 | +type Http struct { |
| 18 | + address string |
| 19 | + nextID int |
| 20 | + debug bool |
| 21 | + eventCh chan string |
| 22 | +} |
| 23 | + |
| 24 | +// NewHttp creates a new Http transport that will execute the given command. |
| 25 | +// It communicates with the command using JSON-RPC over HTTP. |
| 26 | +// Currently Http transport is implements MCP's Final draft version 2024-11-05, |
| 27 | +// https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/transports/#http-with-sse |
| 28 | +func NewHttp(address string) *Http { |
| 29 | + debug := os.Getenv("MCP_DEBUG") == "1" |
| 30 | + |
| 31 | + _, err := url.ParseRequestURI(address) |
| 32 | + if err != nil { |
| 33 | + fmt.Fprintf(os.Stderr, "invalid address: %s", err) |
| 34 | + os.Exit(1) |
| 35 | + } |
| 36 | + |
| 37 | + resp, err := http.Get(address + "/sse") |
| 38 | + if err != nil { |
| 39 | + fmt.Fprintf(os.Stderr, "error sending request: %s", err) |
| 40 | + os.Exit(1) |
| 41 | + } |
| 42 | + |
| 43 | + eventCh := make(chan string, 1) |
| 44 | + |
| 45 | + go func() { |
| 46 | + defer resp.Body.Close() |
| 47 | + reader := bufio.NewReader(resp.Body) |
| 48 | + for { |
| 49 | + select { |
| 50 | + default: |
| 51 | + line, err := reader.ReadString('\n') |
| 52 | + if err != nil { |
| 53 | + fmt.Fprintf(os.Stderr, "SSE read error: %s", err) |
| 54 | + return |
| 55 | + } |
| 56 | + line = strings.TrimSpace(line) |
| 57 | + if debug { |
| 58 | + fmt.Fprintf(os.Stderr, "DEBUG: Received SSE: %s\n", line) |
| 59 | + } |
| 60 | + if strings.HasPrefix(line, "data:") { |
| 61 | + data := strings.TrimSpace(line[5:]) |
| 62 | + select { |
| 63 | + case eventCh <- data: |
| 64 | + default: |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + }() |
| 70 | + |
| 71 | + // First event we receive from SSE is the message address. We will use this endpoint to keep |
| 72 | + // a session alive. |
| 73 | + var messageAddress string |
| 74 | + select { |
| 75 | + case msg := <-eventCh: |
| 76 | + messageAddress = msg |
| 77 | + case <-time.After(10 * time.Second): |
| 78 | + fmt.Errorf("timeout waiting for SSE response") |
| 79 | + os.Exit(1) |
| 80 | + } |
| 81 | + |
| 82 | + return &Http{ |
| 83 | + // Use the SSE message address as the base address for the HTTP transport |
| 84 | + address: address + messageAddress, |
| 85 | + nextID: 1, |
| 86 | + debug: debug, |
| 87 | + eventCh: eventCh, |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Execute implements the Transport via JSON-RPC over HTTP. |
| 92 | +func (t *Http) Execute(method string, params any) (map[string]any, error) { |
| 93 | + if t.debug { |
| 94 | + fmt.Fprintf(os.Stderr, "DEBUG: Connecting to server: %s\n", t.address) |
| 95 | + } |
| 96 | + |
| 97 | + request := Request{ |
| 98 | + JSONRPC: "2.0", |
| 99 | + Method: method, |
| 100 | + ID: t.nextID, |
| 101 | + Params: params, |
| 102 | + } |
| 103 | + t.nextID++ |
| 104 | + |
| 105 | + requestJSON, err := json.Marshal(request) |
| 106 | + if err != nil { |
| 107 | + return nil, fmt.Errorf("error marshaling request: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + requestJSON = append(requestJSON, '\n') |
| 111 | + |
| 112 | + if t.debug { |
| 113 | + fmt.Fprintf(os.Stderr, "DEBUG: Sending request: %s\n", string(requestJSON)) |
| 114 | + } |
| 115 | + |
| 116 | + resp, err := http.Post(t.address, "application/json", bytes.NewBuffer(requestJSON)) |
| 117 | + if err != nil { |
| 118 | + return nil, fmt.Errorf("error sending request: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + if t.debug { |
| 122 | + fmt.Fprintf(os.Stderr, "DEBUG: Sent request to server\n") |
| 123 | + } |
| 124 | + |
| 125 | + defer resp.Body.Close() |
| 126 | + |
| 127 | + body, err := io.ReadAll(resp.Body) |
| 128 | + if err != nil { |
| 129 | + return nil, fmt.Errorf("error reading response: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + if t.debug { |
| 133 | + fmt.Fprintf(os.Stderr, "DEBUG: Read from server: %s\n", string(body)) |
| 134 | + } |
| 135 | + |
| 136 | + if len(body) == 0 { |
| 137 | + return nil, fmt.Errorf("no response from server") |
| 138 | + } |
| 139 | + |
| 140 | + // After sending the request, we listen the SSE channel for the response |
| 141 | + var response Response |
| 142 | + select { |
| 143 | + case msg := <-t.eventCh: |
| 144 | + if unmarshalErr := json.Unmarshal([]byte(msg), &response); unmarshalErr != nil { |
| 145 | + return nil, fmt.Errorf("error unmarshaling response: %w, response: %s", unmarshalErr, msg) |
| 146 | + } |
| 147 | + case <-time.After(10 * time.Second): |
| 148 | + fmt.Errorf("timeout waiting for SSE response") |
| 149 | + os.Exit(1) |
| 150 | + } |
| 151 | + |
| 152 | + if response.Error != nil { |
| 153 | + return nil, fmt.Errorf("RPC error %d: %s", response.Error.Code, response.Error.Message) |
| 154 | + } |
| 155 | + |
| 156 | + if t.debug { |
| 157 | + fmt.Fprintf(os.Stderr, "DEBUG: Successfully parsed response\n") |
| 158 | + } |
| 159 | + |
| 160 | + return response.Result, nil |
| 161 | +} |
0 commit comments