mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-03-17 10:25:11 +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:
42
operation/user/slim.go
Normal file
42
operation/user/slim.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
func slimUserDetail(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 slimOrg(o *gitea_sdk.Organization) map[string]any {
|
||||
if o == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]any{
|
||||
"id": o.ID,
|
||||
"name": o.Name,
|
||||
"full_name": o.FullName,
|
||||
"description": o.Description,
|
||||
"avatar_url": o.AvatarURL,
|
||||
"website": o.Website,
|
||||
}
|
||||
}
|
||||
|
||||
func slimOrgs(orgs []*gitea_sdk.Organization) []map[string]any {
|
||||
out := make([]map[string]any, 0, len(orgs))
|
||||
for _, o := range orgs {
|
||||
out = append(out, slimOrg(o))
|
||||
}
|
||||
return out
|
||||
}
|
||||
39
operation/user/slim_test.go
Normal file
39
operation/user/slim_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
func TestSlimUserDetail(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 := slimUserDetail(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 TestSlimUserDetail_Nil(t *testing.T) {
|
||||
if m := slimUserDetail(nil); m != nil {
|
||||
t.Errorf("expected nil for nil user, got %v", m)
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ const (
|
||||
// defaultPage is the default starting page number used for paginated organization listings.
|
||||
defaultPage = 1
|
||||
// defaultPageSize is the default number of organizations per page for paginated queries.
|
||||
defaultPageSize = 100
|
||||
defaultPageSize = 30
|
||||
)
|
||||
|
||||
// Tool is the MCP tool manager instance for registering all MCP tools in this package.
|
||||
@@ -66,16 +66,6 @@ func registerTools() {
|
||||
}
|
||||
}
|
||||
|
||||
// getIntArg parses an integer argument from the MCP request arguments map.
|
||||
// Returns def if missing, not a number, or less than 1. Used for pagination arguments.
|
||||
func getIntArg(req mcp.CallToolRequest, name string, def int) int {
|
||||
v := params.GetOptionalInt(req.GetArguments(), name, int64(def))
|
||||
if v < 1 {
|
||||
return def
|
||||
}
|
||||
return int(v)
|
||||
}
|
||||
|
||||
// GetUserInfoFn is the handler for "get_my_user_info" MCP tool requests.
|
||||
// Logs invocation, fetches current user info from gitea, wraps result for MCP.
|
||||
func GetUserInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
@@ -88,7 +78,7 @@ func GetUserInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolR
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get user info err: %v", err))
|
||||
}
|
||||
return to.TextResult(user)
|
||||
return to.TextResult(slimUserDetail(user))
|
||||
}
|
||||
|
||||
// GetUserOrgsFn is the handler for "get_user_orgs" MCP tool requests.
|
||||
@@ -96,8 +86,7 @@ func GetUserInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolR
|
||||
// performs Gitea organization listing, and wraps the result for MCP.
|
||||
func GetUserOrgsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("[User] Called GetUserOrgsFn")
|
||||
page := getIntArg(req, "page", defaultPage)
|
||||
pageSize := getIntArg(req, "pageSize", defaultPageSize)
|
||||
page, pageSize := params.GetPagination(req.GetArguments(), defaultPageSize)
|
||||
|
||||
opt := gitea_sdk.ListOrgsOptions{
|
||||
ListOptions: gitea_sdk.ListOptions{
|
||||
@@ -113,5 +102,5 @@ func GetUserOrgsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolR
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get user orgs err: %v", err))
|
||||
}
|
||||
return to.TextResult(orgs)
|
||||
return to.TextResult(slimOrgs(orgs))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user