This commit is contained in:
18
pkg/annotation/annotation.go
Normal file
18
pkg/annotation/annotation.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package annotation
|
||||
|
||||
import "github.com/mark3labs/mcp-go/mcp"
|
||||
|
||||
func ReadOnly(title string) mcp.ToolAnnotation {
|
||||
t := true
|
||||
return mcp.ToolAnnotation{Title: title, ReadOnlyHint: &t}
|
||||
}
|
||||
|
||||
func Write(title string) mcp.ToolAnnotation {
|
||||
f := false
|
||||
return mcp.ToolAnnotation{Title: title, ReadOnlyHint: &f}
|
||||
}
|
||||
|
||||
func Destructive(title string) mcp.ToolAnnotation {
|
||||
f, t := false, true
|
||||
return mcp.ToolAnnotation{Title: title, ReadOnlyHint: &f, DestructiveHint: &t}
|
||||
}
|
||||
7
pkg/context/context.go
Normal file
7
pkg/context/context.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package context
|
||||
|
||||
type contextKey string
|
||||
|
||||
const (
|
||||
TokenContextKey = contextKey("token")
|
||||
)
|
||||
14
pkg/flag/flag.go
Normal file
14
pkg/flag/flag.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package flag
|
||||
|
||||
var (
|
||||
Host string
|
||||
Port int
|
||||
Token string
|
||||
Version string
|
||||
Mode string
|
||||
|
||||
Insecure bool
|
||||
ReadOnly bool
|
||||
Debug bool
|
||||
AllowedTools map[string]struct{}
|
||||
)
|
||||
82
pkg/gitea/gitea.go
Normal file
82
pkg/gitea/gitea.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
mcpContext "gitea.com/gitea/gitea-mcp/pkg/context"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
|
||||
"gitea.dev/sdk"
|
||||
)
|
||||
|
||||
var (
|
||||
clientCache sync.Map // token -> *gitea.Client
|
||||
sharedTransOnce sync.Once
|
||||
sharedTrans *http.Transport
|
||||
)
|
||||
|
||||
func sharedTransport() *http.Transport {
|
||||
sharedTransOnce.Do(func() {
|
||||
sharedTrans = http.DefaultTransport.(*http.Transport).Clone()
|
||||
if flag.Insecure {
|
||||
sharedTrans.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec // user-requested insecure mode
|
||||
}
|
||||
})
|
||||
return sharedTrans
|
||||
}
|
||||
|
||||
// NewClient returns a cached *gitea.Client keyed by host+token. The SDK's per-client
|
||||
// version cache and the shared transport let us reuse keep-alive connections
|
||||
// and avoid the SDK's /api/v1/version preflight on every tool call.
|
||||
func NewClient(token string) (*gitea.Client, error) {
|
||||
key := flag.Host + "\x00" + token
|
||||
if v, ok := clientCache.Load(key); ok {
|
||||
return v.(*gitea.Client), nil
|
||||
}
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: sharedTransport(),
|
||||
CheckRedirect: checkRedirect,
|
||||
}
|
||||
opts := []gitea.ClientOption{
|
||||
gitea.SetToken(token),
|
||||
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)
|
||||
}
|
||||
client.SetUserAgent("gitea-mcp-server/" + flag.Version)
|
||||
|
||||
actual, _ := clientCache.LoadOrStore(key, client)
|
||||
return actual.(*gitea.Client), nil
|
||||
}
|
||||
|
||||
// checkRedirect prevents Go from silently changing mutating requests (POST, PATCH, etc.)
|
||||
// to GET when following 301/302/303 redirects, which would drop the request body and
|
||||
// make writes appear to succeed when they didn't.
|
||||
func checkRedirect(_ *http.Request, via []*http.Request) error {
|
||||
if len(via) >= 10 {
|
||||
return errors.New("stopped after 10 redirects")
|
||||
}
|
||||
if via[0].Method != http.MethodGet && via[0].Method != http.MethodHead {
|
||||
return http.ErrUseLastResponse
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ClientFromContext(ctx context.Context) (*gitea.Client, error) {
|
||||
token, ok := ctx.Value(mcpContext.TokenContextKey).(string)
|
||||
if !ok {
|
||||
token = flag.Token
|
||||
}
|
||||
return NewClient(token)
|
||||
}
|
||||
120
pkg/gitea/redirect_test.go
Normal file
120
pkg/gitea/redirect_test.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
)
|
||||
|
||||
func TestCheckRedirect(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
method string
|
||||
wantErr error
|
||||
}{
|
||||
{"allows GET", http.MethodGet, nil},
|
||||
{"allows HEAD", http.MethodHead, nil},
|
||||
{"blocks PATCH", http.MethodPatch, http.ErrUseLastResponse},
|
||||
{"blocks POST", http.MethodPost, http.ErrUseLastResponse},
|
||||
{"blocks PUT", http.MethodPut, http.ErrUseLastResponse},
|
||||
{"blocks DELETE", http.MethodDelete, http.ErrUseLastResponse},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
via := []*http.Request{{Method: tc.method}}
|
||||
err := checkRedirect(nil, via)
|
||||
if err != tc.wantErr {
|
||||
t.Fatalf("expected %v, got %v", tc.wantErr, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("stops after 10 redirects", func(t *testing.T) {
|
||||
via := make([]*http.Request, 10)
|
||||
for i := range via {
|
||||
via[i] = &http.Request{Method: http.MethodGet}
|
||||
}
|
||||
err := checkRedirect(nil, via)
|
||||
if err == nil || err == http.ErrUseLastResponse {
|
||||
t.Fatalf("expected redirect limit error, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestDoJSON_RepoRenameRedirect is a regression test for the bug where a PATCH
|
||||
// request to a renamed repo got a 301 redirect, Go's http.Client silently
|
||||
// changed the method to GET, and the write appeared to succeed without error.
|
||||
func TestDoJSON_RepoRenameRedirect(t *testing.T) {
|
||||
// Simulate a Gitea API that returns 301 for the old repo name (like a renamed repo).
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("PATCH /api/v1/repos/owner/old-name/pulls/1", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/api/v1/repos/owner/new-name/pulls/1", http.StatusMovedPermanently)
|
||||
})
|
||||
mux.HandleFunc("PATCH /api/v1/repos/owner/new-name/pulls/1", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, `{"id":1,"title":"updated"}`)
|
||||
})
|
||||
mux.HandleFunc("GET /api/v1/repos/owner/new-name/pulls/1", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, `{"id":1,"title":"not-updated"}`)
|
||||
})
|
||||
srv := httptest.NewServer(mux)
|
||||
defer srv.Close()
|
||||
|
||||
origHost := flag.Host
|
||||
defer func() { flag.Host = origHost }()
|
||||
flag.Host = srv.URL
|
||||
|
||||
var result map[string]any
|
||||
status, err := DoJSON(context.Background(), http.MethodPatch, "repos/owner/old-name/pulls/1", nil, map[string]string{"title": "updated"}, &result)
|
||||
if err != nil {
|
||||
// The redirect should be blocked, returning the 301 response directly.
|
||||
// DoJSON treats non-2xx as an error, which is the correct behavior.
|
||||
if status != http.StatusMovedPermanently {
|
||||
t.Fatalf("expected status 301, got %d (err: %v)", status, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// If we reach here without error, the redirect was followed. Verify the
|
||||
// method was preserved (title should be "updated", not "not-updated").
|
||||
title, _ := result["title"].(string)
|
||||
if title == "not-updated" {
|
||||
t.Fatal("PATCH was silently converted to GET on 301 redirect — write was lost")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDoJSON_GETRedirectFollowed verifies that GET requests still follow redirects normally.
|
||||
func TestDoJSON_GETRedirectFollowed(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /api/v1/repos/owner/old-name/pulls/1", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/api/v1/repos/owner/new-name/pulls/1", http.StatusMovedPermanently)
|
||||
})
|
||||
mux.HandleFunc("GET /api/v1/repos/owner/new-name/pulls/1", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"id": 1, "title": "found"})
|
||||
})
|
||||
srv := httptest.NewServer(mux)
|
||||
defer srv.Close()
|
||||
|
||||
origHost := flag.Host
|
||||
defer func() { flag.Host = origHost }()
|
||||
flag.Host = srv.URL
|
||||
|
||||
var result map[string]any
|
||||
status, err := DoJSON(context.Background(), http.MethodGet, "repos/owner/old-name/pulls/1", nil, nil, &result)
|
||||
if err != nil {
|
||||
t.Fatalf("GET redirect should be followed, got error: %v (status %d)", err, status)
|
||||
}
|
||||
title, _ := result["title"].(string)
|
||||
if title != "found" {
|
||||
t.Fatalf("expected title 'found', got %q", title)
|
||||
}
|
||||
}
|
||||
184
pkg/gitea/rest.go
Normal file
184
pkg/gitea/rest.go
Normal file
@@ -0,0 +1,184 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
mcpContext "gitea.com/gitea/gitea-mcp/pkg/context"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
)
|
||||
|
||||
const (
|
||||
httpClientTimeout = 60 * time.Second
|
||||
errBodySnippetSize = 8192
|
||||
)
|
||||
|
||||
type HTTPError struct {
|
||||
StatusCode int
|
||||
Body string
|
||||
}
|
||||
|
||||
func (e *HTTPError) Error() string {
|
||||
if e.Body == "" {
|
||||
return fmt.Sprintf("request failed with status %d", e.StatusCode)
|
||||
}
|
||||
return fmt.Sprintf("request failed with status %d: %s", e.StatusCode, e.Body)
|
||||
}
|
||||
|
||||
func tokenFromContext(ctx context.Context) string {
|
||||
if ctx != nil {
|
||||
if token, ok := ctx.Value(mcpContext.TokenContextKey).(string); ok && token != "" {
|
||||
return token
|
||||
}
|
||||
}
|
||||
return flag.Token
|
||||
}
|
||||
|
||||
var (
|
||||
restClientOnce sync.Once
|
||||
restClient *http.Client
|
||||
)
|
||||
|
||||
func restHTTPClient() *http.Client {
|
||||
restClientOnce.Do(func() {
|
||||
restClient = &http.Client{
|
||||
Transport: sharedTransport(),
|
||||
Timeout: httpClientTimeout,
|
||||
CheckRedirect: checkRedirect,
|
||||
}
|
||||
})
|
||||
return restClient
|
||||
}
|
||||
|
||||
func buildAPIURL(path string, query url.Values) (string, error) {
|
||||
host := strings.TrimRight(flag.Host, "/")
|
||||
if host == "" {
|
||||
return "", errors.New("gitea host is empty")
|
||||
}
|
||||
p := strings.TrimLeft(path, "/")
|
||||
u, err := url.Parse(fmt.Sprintf("%s/api/v1/%s", host, p))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if query != nil {
|
||||
u.RawQuery = query.Encode()
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
// DoJSON performs an API request and decodes a JSON response into respOut (if non-nil).
|
||||
// It returns the HTTP status code.
|
||||
func DoJSON(ctx context.Context, method, path string, query url.Values, body, respOut any) (int, error) {
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("marshal request body: %w", err)
|
||||
}
|
||||
bodyReader = bytes.NewReader(b)
|
||||
}
|
||||
|
||||
u, err := buildAPIURL(path, query)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, method, u, bodyReader)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
token := tokenFromContext(ctx)
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "token "+token)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
client := restHTTPClient()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("do request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
bodySnippet, _ := io.ReadAll(io.LimitReader(resp.Body, errBodySnippetSize))
|
||||
return resp.StatusCode, &HTTPError{StatusCode: resp.StatusCode, Body: strings.TrimSpace(string(bodySnippet))}
|
||||
}
|
||||
|
||||
if respOut == nil {
|
||||
_, _ = io.Copy(io.Discard, resp.Body) // best-effort
|
||||
return resp.StatusCode, nil
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(respOut); err != nil {
|
||||
return resp.StatusCode, fmt.Errorf("decode response: %w", err)
|
||||
}
|
||||
return resp.StatusCode, nil
|
||||
}
|
||||
|
||||
// DoBytes performs an API request and returns the raw response bytes.
|
||||
// It returns the HTTP status code.
|
||||
func DoBytes(ctx context.Context, method, path string, query url.Values, body any, accept string) ([]byte, int, error) {
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("marshal request body: %w", err)
|
||||
}
|
||||
bodyReader = bytes.NewReader(b)
|
||||
}
|
||||
|
||||
u, err := buildAPIURL(path, query)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, method, u, bodyReader)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
token := tokenFromContext(ctx)
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "token "+token)
|
||||
}
|
||||
if accept != "" {
|
||||
req.Header.Set("Accept", accept)
|
||||
}
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
client := restHTTPClient()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("do request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, resp.StatusCode, fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
bodySnippet := respBytes
|
||||
if len(bodySnippet) > errBodySnippetSize {
|
||||
bodySnippet = bodySnippet[:errBodySnippetSize]
|
||||
}
|
||||
return nil, resp.StatusCode, &HTTPError{StatusCode: resp.StatusCode, Body: strings.TrimSpace(string(bodySnippet))}
|
||||
}
|
||||
|
||||
return respBytes, resp.StatusCode, nil
|
||||
}
|
||||
30
pkg/gitea/rest_test.go
Normal file
30
pkg/gitea/rest_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
mcpContext "gitea.com/gitea/gitea-mcp/pkg/context"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
)
|
||||
|
||||
func TestTokenFromContext(t *testing.T) {
|
||||
orig := flag.Token
|
||||
defer func() { flag.Token = orig }()
|
||||
|
||||
flag.Token = "flag-token"
|
||||
|
||||
t.Run("context token wins", func(t *testing.T) {
|
||||
ctx := context.WithValue(context.Background(), mcpContext.TokenContextKey, "ctx-token")
|
||||
if got := tokenFromContext(ctx); got != "ctx-token" {
|
||||
t.Fatalf("tokenFromContext() = %q, want %q", got, "ctx-token")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("fallback to flag token", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
if got := tokenFromContext(ctx); got != "flag-token" {
|
||||
t.Fatalf("tokenFromContext() = %q, want %q", got, "flag-token")
|
||||
}
|
||||
})
|
||||
}
|
||||
120
pkg/log/log.go
Normal file
120
pkg/log/log.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultLoggerOnce sync.Once
|
||||
defaultLogger *zap.Logger
|
||||
)
|
||||
|
||||
func Default() *zap.Logger {
|
||||
defaultLoggerOnce.Do(func() {
|
||||
if defaultLogger != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ec := zap.NewProductionEncoderConfig()
|
||||
ec.EncodeTime = zapcore.TimeEncoderOfLayout(time.DateTime)
|
||||
ec.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
|
||||
var ws zapcore.WriteSyncer
|
||||
var wss []zapcore.WriteSyncer
|
||||
|
||||
home, _ := os.UserHomeDir()
|
||||
if home == "" {
|
||||
home = os.TempDir()
|
||||
}
|
||||
|
||||
logDir := home + "/.gitea-mcp"
|
||||
if err := os.MkdirAll(logDir, 0o700); err != nil {
|
||||
// Fallback to temp directory if creation fails
|
||||
logDir = os.TempDir()
|
||||
}
|
||||
|
||||
wss = append(wss, zapcore.AddSync(&lumberjack.Logger{
|
||||
Filename: logDir + "/gitea-mcp.log",
|
||||
MaxSize: 100,
|
||||
MaxBackups: 10,
|
||||
MaxAge: 30,
|
||||
}))
|
||||
|
||||
if flag.Mode == "http" {
|
||||
wss = append(wss, zapcore.AddSync(os.Stdout))
|
||||
}
|
||||
|
||||
ws = zapcore.NewMultiWriteSyncer(wss...)
|
||||
|
||||
enc := zapcore.NewConsoleEncoder(ec)
|
||||
var level zapcore.Level
|
||||
if flag.Debug {
|
||||
level = zapcore.DebugLevel
|
||||
} else {
|
||||
level = zapcore.InfoLevel
|
||||
}
|
||||
core := zapcore.NewCore(enc, ws, level)
|
||||
options := []zap.Option{
|
||||
zap.AddStacktrace(zapcore.DPanicLevel),
|
||||
zap.AddCaller(),
|
||||
zap.AddCallerSkip(1),
|
||||
}
|
||||
defaultLogger = zap.New(core, options...)
|
||||
})
|
||||
|
||||
return defaultLogger
|
||||
}
|
||||
|
||||
func SetDefault(logger *zap.Logger) {
|
||||
if logger != nil {
|
||||
defaultLogger = logger
|
||||
}
|
||||
}
|
||||
|
||||
func Debug(msg string, fields ...zap.Field) {
|
||||
Default().Debug(msg, fields...)
|
||||
}
|
||||
|
||||
func Info(msg string, fields ...zap.Field) {
|
||||
Default().Info(msg, fields...)
|
||||
}
|
||||
|
||||
func Warn(msg string, fields ...zap.Field) {
|
||||
Default().Warn(msg, fields...)
|
||||
}
|
||||
|
||||
func Error(msg string, fields ...zap.Field) {
|
||||
Default().Error(msg, fields...)
|
||||
}
|
||||
|
||||
func Panic(msg string, fields ...zap.Field) {
|
||||
Default().Panic(msg, fields...)
|
||||
}
|
||||
|
||||
func Debugf(format string, args ...any) {
|
||||
Default().Sugar().Debugf(format, args...)
|
||||
}
|
||||
|
||||
func Infof(format string, args ...any) {
|
||||
Default().Sugar().Infof(format, args...)
|
||||
}
|
||||
|
||||
func Warnf(format string, args ...any) {
|
||||
Default().Sugar().Warnf(format, args...)
|
||||
}
|
||||
|
||||
func Errorf(format string, args ...any) {
|
||||
Default().Sugar().Errorf(format, args...)
|
||||
}
|
||||
|
||||
func Fatalf(format string, args ...any) {
|
||||
Default().Sugar().Fatalf(format, args...)
|
||||
}
|
||||
156
pkg/params/params.go
Normal file
156
pkg/params/params.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package params
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Shared parameter description strings used across tools. Extracted to avoid
|
||||
// repeating the same boilerplate in every tool schema (saves tokens in the
|
||||
// tool list sent to MCP clients).
|
||||
const (
|
||||
OwnerDesc = "repo owner"
|
||||
RepoDesc = "repo name"
|
||||
PageDesc = "page"
|
||||
PaginationDesc = "results per page"
|
||||
)
|
||||
|
||||
// GetString extracts a required string parameter. Empty strings are treated as missing.
|
||||
func GetString(args map[string]any, key string) (string, error) {
|
||||
val, ok := args[key].(string)
|
||||
if !ok || val == "" {
|
||||
return "", fmt.Errorf("%s is required", key)
|
||||
}
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func GetOptionalString(args map[string]any, key, defaultVal string) string {
|
||||
if val, ok := args[key].(string); ok {
|
||||
return val
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func GetStringSlice(args map[string]any, key string) []string {
|
||||
val, ok := args[key]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
sliceVal, ok := val.([]any)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(sliceVal))
|
||||
for _, item := range sliceVal {
|
||||
if s, ok := item.(string); ok {
|
||||
out = append(out, s)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func GetPagination(args map[string]any, defaultPageSize int64) (page, pageSize int) {
|
||||
return int(GetOptionalInt(args, "page", 1)), int(GetOptionalInt(args, "per_page", defaultPageSize))
|
||||
}
|
||||
|
||||
// ToInt64 accepts float64 (JSON number) and string representations.
|
||||
func ToInt64(val any) (int64, bool) {
|
||||
switch v := val.(type) {
|
||||
case float64:
|
||||
return int64(v), true
|
||||
case string:
|
||||
i, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return i, true
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
|
||||
// GetIndex extracts a required integer. Accepts numeric or string forms — LLM callers
|
||||
// often pass identifiers like issue/PR numbers as strings.
|
||||
func GetIndex(args map[string]any, key string) (int64, error) {
|
||||
val, exists := args[key]
|
||||
if !exists {
|
||||
return 0, fmt.Errorf("%s is required", key)
|
||||
}
|
||||
|
||||
if i, ok := ToInt64(val); ok {
|
||||
return i, nil
|
||||
}
|
||||
|
||||
if s, ok := val.(string); ok {
|
||||
return 0, fmt.Errorf("%s must be a valid integer (got %q)", key, s)
|
||||
}
|
||||
|
||||
return 0, fmt.Errorf("%s must be a number or numeric string", key)
|
||||
}
|
||||
|
||||
func GetInt64Slice(args map[string]any, key string) ([]int64, error) {
|
||||
raw, ok := args[key].([]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s (array of IDs) is required", key)
|
||||
}
|
||||
out := make([]int64, 0, len(raw))
|
||||
for _, v := range raw {
|
||||
id, ok := ToInt64(v)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid ID in %s array", key)
|
||||
}
|
||||
out = append(out, id)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// GetOptionalTime parses RFC3339, returning nil if missing or unparseable.
|
||||
func GetOptionalTime(args map[string]any, key string) *time.Time {
|
||||
val, ok := args[key].(string)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if t, err := time.Parse(time.RFC3339, val); err == nil {
|
||||
return &t
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetOptionalInt(args map[string]any, key string, defaultVal int64) int64 {
|
||||
val, exists := args[key]
|
||||
if !exists {
|
||||
return defaultVal
|
||||
}
|
||||
if i, ok := ToInt64(val); ok {
|
||||
return i
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// GetOptionalBoolPtr is for SDK fields where nil/false/true are distinct (e.g. "no change" vs "set to false").
|
||||
func GetOptionalBoolPtr(args map[string]any, key string) *bool {
|
||||
if v, ok := args[key].(bool); ok {
|
||||
return &v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetOptionalStringPtr returns nil when the key is missing OR the value is an empty string.
|
||||
// Use this for create/fork-style fields where "" is meaningless (e.g. fork target name).
|
||||
func GetOptionalStringPtr(args map[string]any, key string) *string {
|
||||
if v, ok := args[key].(string); ok && v != "" {
|
||||
return &v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPresentStringPtr returns &v whenever the key is present as a string, including "".
|
||||
// Use this for PATCH-style fields where the SDK distinguishes "no change" (nil) from
|
||||
// "set to empty" (&""), e.g. clearing an issue body or label description.
|
||||
func GetPresentStringPtr(args map[string]any, key string) *string {
|
||||
if v, ok := args[key].(string); ok {
|
||||
return &v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
208
pkg/params/params_test.go
Normal file
208
pkg/params/params_test.go
Normal file
@@ -0,0 +1,208 @@
|
||||
package params
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetPagination(t *testing.T) {
|
||||
page, perPage := GetPagination(map[string]any{"page": float64(2), "per_page": float64(40)}, 30)
|
||||
if page != 2 || perPage != 40 {
|
||||
t.Errorf("GetPagination = (%d, %d), want (2, 40)", page, perPage)
|
||||
}
|
||||
page, perPage = GetPagination(map[string]any{}, 30)
|
||||
if page != 1 || perPage != 30 {
|
||||
t.Errorf("GetPagination defaults = (%d, %d), want (1, 30)", page, perPage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToInt64(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
val any
|
||||
want int64
|
||||
ok bool
|
||||
}{
|
||||
{"float64", float64(42), 42, true},
|
||||
{"float64 zero", float64(0), 0, true},
|
||||
{"float64 negative", float64(-5), -5, true},
|
||||
{"string", "123", 123, true},
|
||||
{"string zero", "0", 0, true},
|
||||
{"string negative", "-10", -10, true},
|
||||
{"invalid string", "abc", 0, false},
|
||||
{"decimal string", "1.5", 0, false},
|
||||
{"bool", true, 0, false},
|
||||
{"nil", nil, 0, false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, ok := ToInt64(tt.val)
|
||||
if ok != tt.ok {
|
||||
t.Errorf("ToInt64() ok = %v, want %v", ok, tt.ok)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("ToInt64() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOptionalInt(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args map[string]any
|
||||
key string
|
||||
defaultVal int64
|
||||
want int64
|
||||
}{
|
||||
{"present float64", map[string]any{"page": float64(3)}, "page", 1, 3},
|
||||
{"present string", map[string]any{"page": "5"}, "page", 1, 5},
|
||||
{"missing key", map[string]any{}, "page", 1, 1},
|
||||
{"invalid string", map[string]any{"page": "abc"}, "page", 1, 1},
|
||||
{"invalid type", map[string]any{"page": true}, "page", 1, 1},
|
||||
{"zero value", map[string]any{"id": float64(0)}, "id", 99, 0},
|
||||
{"string zero", map[string]any{"id": "0"}, "id", 99, 0},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := GetOptionalInt(tt.args, tt.key, tt.defaultVal)
|
||||
if got != tt.want {
|
||||
t.Errorf("GetOptionalInt() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOptionalStringPtr(t *testing.T) {
|
||||
if p := GetOptionalStringPtr(map[string]any{}, "k"); p != nil {
|
||||
t.Errorf("missing key: got %v, want nil", p)
|
||||
}
|
||||
if p := GetOptionalStringPtr(map[string]any{"k": ""}, "k"); p != nil {
|
||||
t.Errorf("empty string: got %v, want nil", p)
|
||||
}
|
||||
if p := GetOptionalStringPtr(map[string]any{"k": 42}, "k"); p != nil {
|
||||
t.Errorf("non-string: got %v, want nil", p)
|
||||
}
|
||||
if p := GetOptionalStringPtr(map[string]any{"k": nil}, "k"); p != nil {
|
||||
t.Errorf("nil value (JSON null): got %v, want nil", p)
|
||||
}
|
||||
if p := GetOptionalStringPtr(map[string]any{"k": "x"}, "k"); p == nil || *p != "x" {
|
||||
t.Errorf("non-empty: got %v, want &\"x\"", p)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPresentStringPtr(t *testing.T) {
|
||||
if p := GetPresentStringPtr(map[string]any{}, "k"); p != nil {
|
||||
t.Errorf("missing key: got %v, want nil", p)
|
||||
}
|
||||
if p := GetPresentStringPtr(map[string]any{"k": 42}, "k"); p != nil {
|
||||
t.Errorf("non-string: got %v, want nil", p)
|
||||
}
|
||||
if p := GetPresentStringPtr(map[string]any{"k": nil}, "k"); p != nil {
|
||||
t.Errorf("nil value (JSON null): got %v, want nil", p)
|
||||
}
|
||||
if p := GetPresentStringPtr(map[string]any{"k": ""}, "k"); p == nil || *p != "" {
|
||||
t.Errorf("empty string: got %v, want &\"\"", p)
|
||||
}
|
||||
if p := GetPresentStringPtr(map[string]any{"k": "x"}, "k"); p == nil || *p != "x" {
|
||||
t.Errorf("non-empty: got %v, want &\"x\"", p)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIndex(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args map[string]any
|
||||
key string
|
||||
wantIndex int64
|
||||
wantErr bool
|
||||
errMsg string
|
||||
}{
|
||||
{
|
||||
name: "valid float64",
|
||||
args: map[string]any{"index": float64(123)},
|
||||
key: "index",
|
||||
wantIndex: 123,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "valid string",
|
||||
args: map[string]any{"index": "456"},
|
||||
key: "index",
|
||||
wantIndex: 456,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "valid string with large number",
|
||||
args: map[string]any{"index": "999999"},
|
||||
key: "index",
|
||||
wantIndex: 999999,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "missing parameter",
|
||||
args: map[string]any{},
|
||||
key: "index",
|
||||
wantErr: true,
|
||||
errMsg: "index is required",
|
||||
},
|
||||
{
|
||||
name: "invalid string (not a number)",
|
||||
args: map[string]any{"index": "abc"},
|
||||
key: "index",
|
||||
wantErr: true,
|
||||
errMsg: "must be a valid integer",
|
||||
},
|
||||
{
|
||||
name: "invalid string (decimal)",
|
||||
args: map[string]any{"index": "12.34"},
|
||||
key: "index",
|
||||
wantErr: true,
|
||||
errMsg: "must be a valid integer",
|
||||
},
|
||||
{
|
||||
name: "invalid type (bool)",
|
||||
args: map[string]any{"index": true},
|
||||
key: "index",
|
||||
wantErr: true,
|
||||
errMsg: "must be a number or numeric string",
|
||||
},
|
||||
{
|
||||
name: "invalid type (map)",
|
||||
args: map[string]any{"index": map[string]string{"foo": "bar"}},
|
||||
key: "index",
|
||||
wantErr: true,
|
||||
errMsg: "must be a number or numeric string",
|
||||
},
|
||||
{
|
||||
name: "custom key name",
|
||||
args: map[string]any{"pr_index": "789"},
|
||||
key: "pr_index",
|
||||
wantIndex: 789,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotIndex, err := GetIndex(tt.args, tt.key)
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Errorf("GetIndex() expected error but got nil")
|
||||
return
|
||||
}
|
||||
if tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
|
||||
t.Errorf("GetIndex() error = %v, want error containing %q", err, tt.errMsg)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("GetIndex() unexpected error = %v", err)
|
||||
return
|
||||
}
|
||||
if gotIndex != tt.wantIndex {
|
||||
t.Errorf("GetIndex() = %v, want %v", gotIndex, tt.wantIndex)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
135
pkg/slim/slim.go
Normal file
135
pkg/slim/slim.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package slim
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
gitea_sdk "gitea.dev/sdk"
|
||||
)
|
||||
|
||||
func UserLogin(u *gitea_sdk.User) string {
|
||||
if u == nil {
|
||||
return ""
|
||||
}
|
||||
return u.UserName
|
||||
}
|
||||
|
||||
func UserLogins(users []*gitea_sdk.User) []string {
|
||||
if len(users) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(users))
|
||||
for _, u := range users {
|
||||
if u != nil {
|
||||
out = append(out, u.UserName)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func LabelNames(labels []*gitea_sdk.Label) []string {
|
||||
if len(labels) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(labels))
|
||||
for _, l := range labels {
|
||||
if l != nil {
|
||||
out = append(out, l.Name)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func BodyWithAttachments(body string, atts []*gitea_sdk.Attachment) string {
|
||||
links := make([]string, 0, len(atts))
|
||||
for _, a := range atts {
|
||||
if a == nil || a.DownloadURL == "" {
|
||||
continue
|
||||
}
|
||||
links = append(links, fmt.Sprintf("[%s](%s)", a.Name, a.DownloadURL))
|
||||
}
|
||||
if len(links) == 0 {
|
||||
return body
|
||||
}
|
||||
joined := strings.Join(links, "\n")
|
||||
if body == "" {
|
||||
return joined
|
||||
}
|
||||
return body + "\n\n" + joined
|
||||
}
|
||||
|
||||
func UserDetail(u *gitea_sdk.User) map[string]any {
|
||||
if u == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]any{
|
||||
"id": u.ID,
|
||||
"login": u.UserName,
|
||||
"full_name": u.FullName,
|
||||
"email": u.Email,
|
||||
"avatar_url": u.AvatarURL,
|
||||
"html_url": u.HTMLURL,
|
||||
"is_admin": u.IsAdmin,
|
||||
}
|
||||
}
|
||||
|
||||
func Repo(r *gitea_sdk.Repository) map[string]any {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
m := map[string]any{
|
||||
"id": r.ID,
|
||||
"full_name": r.FullName,
|
||||
"description": r.Description,
|
||||
"html_url": r.HTMLURL,
|
||||
"clone_url": r.CloneURL,
|
||||
"ssh_url": r.SSHURL,
|
||||
"default_branch": r.DefaultBranch,
|
||||
"private": r.Private,
|
||||
"fork": r.Fork,
|
||||
"archived": r.Archived,
|
||||
"language": r.Language,
|
||||
"stars_count": r.Stars,
|
||||
"forks_count": r.Forks,
|
||||
"open_issues_count": r.OpenIssues,
|
||||
"open_pr_counter": r.OpenPulls,
|
||||
"created_at": r.Created,
|
||||
"updated_at": r.Updated,
|
||||
}
|
||||
if r.Owner != nil {
|
||||
m["owner"] = r.Owner.UserName
|
||||
}
|
||||
if len(r.Topics) > 0 {
|
||||
m["topics"] = r.Topics
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func Repos(repos []*gitea_sdk.Repository) []map[string]any {
|
||||
out := make([]map[string]any, 0, len(repos))
|
||||
for _, r := range repos {
|
||||
out = append(out, Repo(r))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func Label(l *gitea_sdk.Label) map[string]any {
|
||||
if l == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]any{
|
||||
"id": l.ID,
|
||||
"name": l.Name,
|
||||
"color": l.Color,
|
||||
"description": l.Description,
|
||||
"exclusive": l.Exclusive,
|
||||
}
|
||||
}
|
||||
|
||||
func Labels(labels []*gitea_sdk.Label) []map[string]any {
|
||||
out := make([]map[string]any, 0, len(labels))
|
||||
for _, l := range labels {
|
||||
out = append(out, Label(l))
|
||||
}
|
||||
return out
|
||||
}
|
||||
110
pkg/slim/slim_test.go
Normal file
110
pkg/slim/slim_test.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package slim
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
gitea_sdk "gitea.dev/sdk"
|
||||
)
|
||||
|
||||
func TestUserDetail(t *testing.T) {
|
||||
u := &gitea_sdk.User{
|
||||
ID: 42,
|
||||
UserName: "alice",
|
||||
FullName: "Alice Smith",
|
||||
Email: "alice@example.com",
|
||||
AvatarURL: "https://gitea.com/avatars/42",
|
||||
HTMLURL: "https://gitea.com/alice",
|
||||
IsAdmin: true,
|
||||
}
|
||||
m := UserDetail(u)
|
||||
|
||||
if m["id"] != int64(42) {
|
||||
t.Errorf("expected id 42, got %v", m["id"])
|
||||
}
|
||||
if m["login"] != "alice" {
|
||||
t.Errorf("expected login alice, got %v", m["login"])
|
||||
}
|
||||
if m["full_name"] != "Alice Smith" {
|
||||
t.Errorf("expected full_name Alice Smith, got %v", m["full_name"])
|
||||
}
|
||||
if m["is_admin"] != true {
|
||||
t.Errorf("expected is_admin true, got %v", m["is_admin"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserDetail_Nil(t *testing.T) {
|
||||
if m := UserDetail(nil); m != nil {
|
||||
t.Errorf("expected nil for nil user, got %v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLabel(t *testing.T) {
|
||||
l := &gitea_sdk.Label{
|
||||
ID: 1,
|
||||
Name: "bug",
|
||||
Color: "#d73a4a",
|
||||
Description: "Something isn't working",
|
||||
Exclusive: false,
|
||||
}
|
||||
|
||||
m := Label(l)
|
||||
if m["name"] != "bug" {
|
||||
t.Errorf("expected name bug, got %v", m["name"])
|
||||
}
|
||||
if m["color"] != "#d73a4a" {
|
||||
t.Errorf("expected color, got %v", m["color"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepo(t *testing.T) {
|
||||
r := &gitea_sdk.Repository{
|
||||
ID: 1,
|
||||
FullName: "org/repo",
|
||||
Description: "A test repo",
|
||||
HTMLURL: "https://gitea.com/org/repo",
|
||||
CloneURL: "https://gitea.com/org/repo.git",
|
||||
SSHURL: "git@gitea.com:org/repo.git",
|
||||
DefaultBranch: "main",
|
||||
Language: "Go",
|
||||
Stars: 10,
|
||||
Forks: 2,
|
||||
Owner: &gitea_sdk.User{UserName: "org"},
|
||||
Topics: []string{"mcp", "gitea"},
|
||||
}
|
||||
|
||||
m := Repo(r)
|
||||
|
||||
if m["full_name"] != "org/repo" {
|
||||
t.Errorf("expected full_name org/repo, got %v", m["full_name"])
|
||||
}
|
||||
if m["owner"] != "org" {
|
||||
t.Errorf("expected owner org, got %v", m["owner"])
|
||||
}
|
||||
topics := m["topics"].([]string)
|
||||
if len(topics) != 2 {
|
||||
t.Errorf("expected 2 topics, got %d", len(topics))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBodyWithAttachments(t *testing.T) {
|
||||
atts := []*gitea_sdk.Attachment{
|
||||
{Name: "shot.png", DownloadURL: "https://example/shot.png"},
|
||||
{Name: "log.txt", DownloadURL: "https://example/log.txt"},
|
||||
}
|
||||
got := BodyWithAttachments("see attached", atts)
|
||||
want := "see attached\n\n[shot.png](https://example/shot.png)\n[log.txt](https://example/log.txt)"
|
||||
if got != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
|
||||
if got := BodyWithAttachments("only body", nil); got != "only body" {
|
||||
t.Errorf("nil attachments should return body unchanged, got %q", got)
|
||||
}
|
||||
if got := BodyWithAttachments("", atts); got != "[shot.png](https://example/shot.png)\n[log.txt](https://example/log.txt)" {
|
||||
t.Errorf("empty body should drop separator, got %q", got)
|
||||
}
|
||||
skipped := []*gitea_sdk.Attachment{nil, {Name: "noop", DownloadURL: ""}}
|
||||
if got := BodyWithAttachments("body", skipped); got != "body" {
|
||||
t.Errorf("nil/empty-URL attachments should be skipped, got %q", got)
|
||||
}
|
||||
}
|
||||
27
pkg/to/to.go
Normal file
27
pkg/to/to.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package to
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
||||
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
)
|
||||
|
||||
func TextResult(v any) (*mcp.CallToolResult, error) {
|
||||
resultBytes, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal result err: %v", err)
|
||||
}
|
||||
if flag.Debug {
|
||||
log.Debugf("Text Result: %s", string(resultBytes))
|
||||
}
|
||||
return mcp.NewToolResultText(string(resultBytes)), nil
|
||||
}
|
||||
|
||||
func ErrorResult(err error) (*mcp.CallToolResult, error) {
|
||||
log.Errorf("%s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
78
pkg/tool/tool.go
Normal file
78
pkg/tool/tool.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package tool
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
||||
|
||||
"github.com/mark3labs/mcp-go/server"
|
||||
)
|
||||
|
||||
type Tool struct {
|
||||
write []server.ServerTool
|
||||
read []server.ServerTool
|
||||
}
|
||||
|
||||
func New() *Tool {
|
||||
return &Tool{
|
||||
write: make([]server.ServerTool, 0, 100),
|
||||
read: make([]server.ServerTool, 0, 100),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Tool) RegisterWrite(s server.ServerTool) {
|
||||
t.write = append(t.write, s)
|
||||
}
|
||||
|
||||
func (t *Tool) RegisterRead(s server.ServerTool) {
|
||||
t.read = append(t.read, s)
|
||||
}
|
||||
|
||||
func (t *Tool) Tools() []server.ServerTool {
|
||||
all := make([]server.ServerTool, 0, len(t.write)+len(t.read))
|
||||
if !flag.ReadOnly {
|
||||
all = append(all, t.write...)
|
||||
}
|
||||
all = append(all, t.read...)
|
||||
if len(flag.AllowedTools) == 0 {
|
||||
return all
|
||||
}
|
||||
filtered := make([]server.ServerTool, 0, len(all))
|
||||
for _, st := range all {
|
||||
if _, ok := flag.AllowedTools[st.Tool.Name]; ok {
|
||||
filtered = append(filtered, st)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
// WarnUnmatchedAllowedTools logs any names in flag.AllowedTools that don't
|
||||
// match a tool registered on any of the given domains. No-op if the allowlist
|
||||
// is empty.
|
||||
func WarnUnmatchedAllowedTools(domains ...*Tool) {
|
||||
if len(flag.AllowedTools) == 0 {
|
||||
return
|
||||
}
|
||||
known := map[string]struct{}{}
|
||||
for _, d := range domains {
|
||||
for _, st := range d.read {
|
||||
known[st.Tool.Name] = struct{}{}
|
||||
}
|
||||
for _, st := range d.write {
|
||||
known[st.Tool.Name] = struct{}{}
|
||||
}
|
||||
}
|
||||
var unmatched []string
|
||||
for name := range flag.AllowedTools {
|
||||
if _, ok := known[name]; !ok {
|
||||
unmatched = append(unmatched, name)
|
||||
}
|
||||
}
|
||||
if len(unmatched) == 0 {
|
||||
return
|
||||
}
|
||||
slices.Sort(unmatched)
|
||||
log.Warnf("Unknown tools in --tools allowlist (ignored): %s", strings.Join(unmatched, ", "))
|
||||
}
|
||||
100
pkg/tool/tool_test.go
Normal file
100
pkg/tool/tool_test.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package tool
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
"github.com/mark3labs/mcp-go/server"
|
||||
)
|
||||
|
||||
func makeTool(name string) server.ServerTool {
|
||||
return server.ServerTool{Tool: mcp.NewTool(name)}
|
||||
}
|
||||
|
||||
func names(sts []server.ServerTool) []string {
|
||||
out := make([]string, len(sts))
|
||||
for i, st := range sts {
|
||||
out[i] = st.Tool.Name
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestTools(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
readOnly bool
|
||||
allowed map[string]struct{}
|
||||
read []string
|
||||
write []string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "no filters returns write then read",
|
||||
read: []string{"r1", "r2"},
|
||||
write: []string{"w1", "w2"},
|
||||
want: []string{"w1", "w2", "r1", "r2"},
|
||||
},
|
||||
{
|
||||
name: "read-only excludes write",
|
||||
readOnly: true,
|
||||
read: []string{"r1", "r2"},
|
||||
write: []string{"w1"},
|
||||
want: []string{"r1", "r2"},
|
||||
},
|
||||
{
|
||||
name: "allowlist keeps only listed",
|
||||
allowed: map[string]struct{}{"r1": {}, "w1": {}},
|
||||
read: []string{"r1", "r2"},
|
||||
write: []string{"w1", "w2"},
|
||||
want: []string{"w1", "r1"},
|
||||
},
|
||||
{
|
||||
name: "allowlist intersected with read-only drops write entries",
|
||||
readOnly: true,
|
||||
allowed: map[string]struct{}{"r1": {}, "w1": {}},
|
||||
read: []string{"r1", "r2"},
|
||||
write: []string{"w1", "w2"},
|
||||
want: []string{"r1"},
|
||||
},
|
||||
{
|
||||
name: "allowlist with only unknown names returns empty",
|
||||
allowed: map[string]struct{}{"unknown": {}},
|
||||
read: []string{"r1"},
|
||||
write: []string{"w1"},
|
||||
want: []string{},
|
||||
},
|
||||
{
|
||||
name: "empty allowlist map passes through",
|
||||
allowed: map[string]struct{}{},
|
||||
read: []string{"r1"},
|
||||
write: []string{"w1"},
|
||||
want: []string{"w1", "r1"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
origRO, origAllow := flag.ReadOnly, flag.AllowedTools
|
||||
t.Cleanup(func() {
|
||||
flag.ReadOnly, flag.AllowedTools = origRO, origAllow
|
||||
})
|
||||
flag.ReadOnly = tt.readOnly
|
||||
flag.AllowedTools = tt.allowed
|
||||
|
||||
tr := New()
|
||||
for _, n := range tt.read {
|
||||
tr.RegisterRead(makeTool(n))
|
||||
}
|
||||
for _, n := range tt.write {
|
||||
tr.RegisterWrite(makeTool(n))
|
||||
}
|
||||
|
||||
got := names(tr.Tools())
|
||||
if !slices.Equal(got, tt.want) {
|
||||
t.Errorf("Tools() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user