mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-03-20 03:45:13 +00:00
feat: slim tool responses (#141)
Reduce token usage by slimming tool responses. Instead of returning full Gitea SDK objects (with nested user/repo objects, avatars, permissions, etc.), each operation now has a colocated `slim.go` that extracts only the fields an LLM needs. List endpoints return even fewer fields than single-item endpoints.
Other changes:
- Add `params` helpers to DRY parameter extraction across 40+ handlers
- Remove `{"Result": ...}` wrapper for flatter responses
- Reduce default pageSize from 100 to 30
Fixes: https://gitea.com/gitea/gitea-mcp/issues/128
*Created by Claude on behalf of @silverwind*
Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/141
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-committed-by: silverwind <me@silverwind.io>
This commit is contained in:
142
operation/repo/slim_test.go
Normal file
142
operation/repo/slim_test.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
func TestSlimRepo(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",
|
||||
Private: false,
|
||||
Fork: false,
|
||||
Archived: false,
|
||||
Language: "Go",
|
||||
Stars: 10,
|
||||
Forks: 2,
|
||||
Owner: &gitea_sdk.User{UserName: "org"},
|
||||
Topics: []string{"mcp", "gitea"},
|
||||
}
|
||||
|
||||
m := slimRepo(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 TestSlimTag(t *testing.T) {
|
||||
tag := &gitea_sdk.Tag{
|
||||
Name: "v1.0.0",
|
||||
Message: "Release v1.0.0",
|
||||
Commit: &gitea_sdk.CommitMeta{SHA: "abc123"},
|
||||
}
|
||||
|
||||
m := slimTag(tag)
|
||||
if m["name"] != "v1.0.0" {
|
||||
t.Errorf("expected name v1.0.0, got %v", m["name"])
|
||||
}
|
||||
if m["message"] != "Release v1.0.0" {
|
||||
t.Errorf("expected message, got %v", m["message"])
|
||||
}
|
||||
|
||||
// List variant omits message
|
||||
list := slimTags([]*gitea_sdk.Tag{tag})
|
||||
if _, ok := list[0]["message"]; ok {
|
||||
t.Error("Tags list should omit message")
|
||||
}
|
||||
if list[0]["name"] != "v1.0.0" {
|
||||
t.Errorf("expected name in list, got %v", list[0]["name"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlimRelease(t *testing.T) {
|
||||
r := &gitea_sdk.Release{
|
||||
ID: 1,
|
||||
TagName: "v1.0.0",
|
||||
Title: "First Release",
|
||||
Note: "Release notes",
|
||||
IsDraft: false,
|
||||
Publisher: &gitea_sdk.User{UserName: "alice"},
|
||||
}
|
||||
|
||||
m := slimRelease(r)
|
||||
if m["tag_name"] != "v1.0.0" {
|
||||
t.Errorf("expected tag_name v1.0.0, got %v", m["tag_name"])
|
||||
}
|
||||
if m["body"] != "Release notes" {
|
||||
t.Errorf("expected body from Note field, got %v", m["body"])
|
||||
}
|
||||
if m["author"] != "alice" {
|
||||
t.Errorf("expected author alice, got %v", m["author"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlimContents(t *testing.T) {
|
||||
content := "package main"
|
||||
encoding := "base64"
|
||||
htmlURL := "https://gitea.com/org/repo/src/branch/main/main.go"
|
||||
c := &gitea_sdk.ContentsResponse{
|
||||
Name: "main.go",
|
||||
Path: "main.go",
|
||||
SHA: "abc123",
|
||||
Type: "file",
|
||||
Size: 12,
|
||||
Content: &content,
|
||||
Encoding: &encoding,
|
||||
HTMLURL: &htmlURL,
|
||||
}
|
||||
|
||||
m := slimContents(c)
|
||||
if m["name"] != "main.go" {
|
||||
t.Errorf("expected name main.go, got %v", m["name"])
|
||||
}
|
||||
if m["content"] != "package main" {
|
||||
t.Errorf("expected content, got %v", m["content"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlimDirEntries(t *testing.T) {
|
||||
entries := []*gitea_sdk.ContentsResponse{
|
||||
{Name: "src", Path: "src", Type: "dir", Size: 0},
|
||||
{Name: "main.go", Path: "main.go", Type: "file", Size: 100},
|
||||
}
|
||||
|
||||
result := slimDirEntries(entries)
|
||||
if len(result) != 2 {
|
||||
t.Fatalf("expected 2 entries, got %d", len(result))
|
||||
}
|
||||
if result[0]["name"] != "src" {
|
||||
t.Errorf("expected first entry name src, got %v", result[0]["name"])
|
||||
}
|
||||
// Dir entries should not have content
|
||||
if _, ok := result[0]["content"]; ok {
|
||||
t.Error("dir entries should not have content field")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlimTags_Nil(t *testing.T) {
|
||||
if r := slimTags(nil); len(r) != 0 {
|
||||
t.Errorf("expected empty slice, got %v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlimReleases_Nil(t *testing.T) {
|
||||
if r := slimReleases(nil); len(r) != 0 {
|
||||
t.Errorf("expected empty slice, got %v", r)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user