mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-08-28 19:17:44 +00:00
feat!: support protocol 2026-07-28 over HTTP and add --bind (#227)
Adds MCP protocol `2026-07-28` over HTTP through the official Go SDK, and validates the `Origin` header on every request as the spec requires. Tool and Gitea failures now come back as an ordinary `tools/call` result carrying `result.isError: true`, the way the SDK's own tool wrapper reports them. Malformed requests, unknown tools or methods, and server faults stay JSON-RPC errors. Adds `-b, --bind` to narrow the listen address. The default still accepts every interface, so this is opt-in hardening. It matters because a request that omits `Authorization` falls back to the server's own token. **Breaking: the HTTP endpoint no longer keeps a session per client.** What changes for a client: 1. `/mcp` accepts `POST` only, and answers `405` to `GET` or `DELETE`. 2. The server neither sends nor accepts `Mcp-Session-Id`, so there is no session handshake to perform. 3. There is no standalone SSE stream and no `Last-Event-ID` resumption. If a response stream breaks, send the whole request again under a new JSON-RPC id. Clients that already speak current streamable HTTP need no changes. Anything relying on the session handshake or the standalone SSE stream should stay on the previous release. --------- Co-authored-by: silverwind <me@silverwind.io> Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/227 Reviewed-by: silverwind <2021+silverwind@noreply.gitea.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package flag
|
||||
|
||||
var (
|
||||
Host string
|
||||
Bind string
|
||||
Port int
|
||||
Token string
|
||||
Version string
|
||||
|
||||
+3
-1
@@ -25,5 +25,7 @@ func TextResult(v any) (*mcp.CallToolResult, error) {
|
||||
|
||||
func ErrorResult(err error) (*mcp.CallToolResult, error) {
|
||||
log.Errorf("%s", err.Error())
|
||||
return nil, err
|
||||
var result mcp.CallToolResult
|
||||
result.SetError(err)
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
+11
-2
@@ -27,7 +27,16 @@ func TestTextResult(t *testing.T) {
|
||||
func TestErrorResult(t *testing.T) {
|
||||
want := errors.New("failed")
|
||||
result, err := ErrorResult(want)
|
||||
if result != nil || !errors.Is(err, want) {
|
||||
t.Errorf("ErrorResult() = (%#v, %v), want (nil, %v)", result, err, want)
|
||||
if err != nil {
|
||||
t.Fatalf("ErrorResult() error = %v", err)
|
||||
}
|
||||
if !result.IsError {
|
||||
t.Error("IsError = false, want true")
|
||||
}
|
||||
if len(result.Content) != 1 {
|
||||
t.Fatalf("len(Content) = %d, want 1", len(result.Content))
|
||||
}
|
||||
if content, ok := result.Content[0].(*mcp.TextContent); !ok || content.Text != want.Error() {
|
||||
t.Errorf("Content[0] = %#v, want text %q", result.Content[0], want)
|
||||
}
|
||||
}
|
||||
|
||||
+20
-11
@@ -68,27 +68,36 @@ func TestMCPHandlerAcceptsAbsentArguments(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMCPHandlerConvertsErrorsAndRecoversPanics(t *testing.T) {
|
||||
func TestMCPHandlerErrorClassification(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
handler Handler
|
||||
name string
|
||||
handler Handler
|
||||
wantCode int64
|
||||
}{
|
||||
{
|
||||
name: "handler error",
|
||||
handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) {
|
||||
return nil, errors.New("failed")
|
||||
},
|
||||
name: "server error",
|
||||
handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) { return nil, errors.New("failed") },
|
||||
wantCode: jsonrpc.CodeInternalError,
|
||||
},
|
||||
{
|
||||
name: "panic",
|
||||
name: "protocol error",
|
||||
handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) {
|
||||
panic("failed")
|
||||
return nil, &jsonrpc.Error{Code: jsonrpc.CodeInvalidParams, Message: "failed"}
|
||||
},
|
||||
wantCode: jsonrpc.CodeInvalidParams,
|
||||
},
|
||||
{
|
||||
name: "panic",
|
||||
handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) { panic("failed") },
|
||||
wantCode: jsonrpc.CodeInternalError,
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
_, err := callTool(test.handler, nil)
|
||||
assertProtocolErrorCode(t, err, jsonrpc.CodeInternalError)
|
||||
result, err := callTool(test.handler, nil)
|
||||
if result != nil {
|
||||
t.Errorf("result = %#v, want nil", result)
|
||||
}
|
||||
assertProtocolErrorCode(t, err, test.wantCode)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+1
-2
@@ -107,8 +107,7 @@ func (s ServerTool) MCPHandler() mcp.ToolHandler {
|
||||
if errors.As(err, &protocolErr) {
|
||||
return nil, err
|
||||
}
|
||||
// Preserve mcp-go behavior; tool-result errors are a separate change.
|
||||
return nil, internalError(err)
|
||||
return nil, internalError(err) // Expected failures never reach here, handlers use CallToolResult.
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user