mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2025-09-13 08:23:15 +00:00
this PR introduces support for per-request authentication tokens in HTTP and SSE modes. The server now inspects incoming requests for an `Authorization: Bearer <token>` header. Previously, the server operated with a single, globally configured Gitea token. This change allows different clients to use their own tokens when communicating with the MCP server, enhancing security and flexibility. To support this, the Gitea API client initialization has been refactored: - The global singleton Gitea client has been removed. - A new `ClientFromContext` function creates a Gitea client on-demand, using a token from the request context if available, and falling back to the globally configured token otherwise. - All tool functions now retrieve the client from the context for each call. The README has also been updated to reflect the new configuration option. Update: #59 Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/89 Reviewed-by: hiifong <i@hiif.ong> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Darren Hoo <darren.hoo@gmail.com> Co-committed-by: Darren Hoo <darren.hoo@gmail.com>
48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package gitea
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
mcpContext "gitea.com/gitea/gitea-mcp/pkg/context"
|
|
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
|
)
|
|
|
|
func NewClient(token string) (*gitea.Client, error) {
|
|
httpClient := &http.Client{
|
|
Transport: http.DefaultTransport,
|
|
}
|
|
|
|
opts := []gitea.ClientOption{
|
|
gitea.SetToken(token),
|
|
}
|
|
if flag.Insecure {
|
|
httpClient.Transport.(*http.Transport).TLSClientConfig = &tls.Config{
|
|
InsecureSkipVerify: true,
|
|
}
|
|
}
|
|
opts = append(opts, gitea.SetHTTPClient(httpClient))
|
|
if flag.Debug {
|
|
opts = append(opts, gitea.SetDebugMode())
|
|
}
|
|
client, err := gitea.NewClient(flag.Host, opts...)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create gitea client err: %w", err)
|
|
}
|
|
|
|
// Set user agent for the client
|
|
client.SetUserAgent(fmt.Sprintf("gitea-mcp-server/%s", flag.Version))
|
|
return client, nil
|
|
}
|
|
|
|
func ClientFromContext(ctx context.Context) (*gitea.Client, error) {
|
|
token, ok := ctx.Value(mcpContext.TokenContextKey).(string)
|
|
if !ok {
|
|
token = flag.Token
|
|
}
|
|
return NewClient(token)
|
|
}
|