mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-29 05:47:37 +08:00
fix: send mcp initialized as notification
This commit is contained in:
@@ -804,8 +804,7 @@ func (c *mcpHTTPClient) initialize(ctx context.Context) error {
|
|||||||
if _, ok := result["protocolVersion"]; !ok {
|
if _, ok := result["protocolVersion"]; !ok {
|
||||||
return fmt.Errorf("mcp server %q initialize missing protocolVersion", c.serverName)
|
return fmt.Errorf("mcp server %q initialize missing protocolVersion", c.serverName)
|
||||||
}
|
}
|
||||||
_, _ = c.request(ctx, "notifications/initialized", map[string]interface{}{})
|
return c.notify(ctx, "notifications/initialized", map[string]interface{}{})
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *mcpHTTPClient) listAll(ctx context.Context, method, field string) (map[string]interface{}, error) {
|
func (c *mcpHTTPClient) listAll(ctx context.Context, method, field string) (map[string]interface{}, error) {
|
||||||
@@ -873,6 +872,33 @@ func (c *mcpHTTPClient) request(ctx context.Context, method string, params map[s
|
|||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *mcpHTTPClient) notify(ctx context.Context, method string, params map[string]interface{}) error {
|
||||||
|
payload := map[string]interface{}{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": method,
|
||||||
|
"params": params,
|
||||||
|
}
|
||||||
|
body, err := json.Marshal(payload)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL, bytes.NewReader(body))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
resp, err := c.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("mcp %s %s failed: %w", c.serverName, method, err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
data, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||||
|
return fmt.Errorf("mcp %s %s failed: http %d %s", c.serverName, method, resp.StatusCode, strings.TrimSpace(string(data)))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *mcpClient) Close() error {
|
func (c *mcpClient) Close() error {
|
||||||
if c == nil || c.cmd == nil {
|
if c == nil || c.cmd == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -373,6 +373,7 @@ func TestMCPToolListTools(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMCPToolHTTPTransport(t *testing.T) {
|
func TestMCPToolHTTPTransport(t *testing.T) {
|
||||||
|
initializedNotified := false
|
||||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
var req map[string]interface{}
|
var req map[string]interface{}
|
||||||
@@ -393,11 +394,13 @@ func TestMCPToolHTTPTransport(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
case "notifications/initialized":
|
case "notifications/initialized":
|
||||||
resp = map[string]interface{}{
|
if _, hasID := req["id"]; hasID {
|
||||||
"jsonrpc": "2.0",
|
http.Error(w, "notification must not include id", http.StatusBadRequest)
|
||||||
"id": id,
|
return
|
||||||
"result": map[string]interface{}{},
|
|
||||||
}
|
}
|
||||||
|
initializedNotified = true
|
||||||
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
return
|
||||||
case "tools/list":
|
case "tools/list":
|
||||||
resp = map[string]interface{}{
|
resp = map[string]interface{}{
|
||||||
"jsonrpc": "2.0",
|
"jsonrpc": "2.0",
|
||||||
@@ -455,6 +458,9 @@ func TestMCPToolHTTPTransport(t *testing.T) {
|
|||||||
if !strings.Contains(out, `"name": "echo"`) {
|
if !strings.Contains(out, `"name": "echo"`) {
|
||||||
t.Fatalf("expected http tool listing, got: %s", out)
|
t.Fatalf("expected http tool listing, got: %s", out)
|
||||||
}
|
}
|
||||||
|
if !initializedNotified {
|
||||||
|
t.Fatal("expected initialized notification to be sent")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMCPToolSSETransport(t *testing.T) {
|
func TestMCPToolSSETransport(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user