mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-03-18 10:55:12 +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:
116
operation/issue/slim.go
Normal file
116
operation/issue/slim.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package issue
|
||||
|
||||
import (
|
||||
gitea_sdk "code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
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 slimIssue(i *gitea_sdk.Issue) map[string]any {
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
m := map[string]any{
|
||||
"number": i.Index,
|
||||
"title": i.Title,
|
||||
"body": i.Body,
|
||||
"state": i.State,
|
||||
"html_url": i.HTMLURL,
|
||||
"user": userLogin(i.Poster),
|
||||
"labels": labelNames(i.Labels),
|
||||
"comments": i.Comments,
|
||||
"created_at": i.Created,
|
||||
"updated_at": i.Updated,
|
||||
"closed_at": i.Closed,
|
||||
}
|
||||
if len(i.Assignees) > 0 {
|
||||
m["assignees"] = userLogins(i.Assignees)
|
||||
}
|
||||
if i.Milestone != nil {
|
||||
m["milestone"] = map[string]any{
|
||||
"id": i.Milestone.ID,
|
||||
"title": i.Milestone.Title,
|
||||
}
|
||||
}
|
||||
if i.PullRequest != nil {
|
||||
m["is_pull"] = true
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func slimIssues(issues []*gitea_sdk.Issue) []map[string]any {
|
||||
out := make([]map[string]any, 0, len(issues))
|
||||
for _, i := range issues {
|
||||
if i == nil {
|
||||
continue
|
||||
}
|
||||
m := map[string]any{
|
||||
"number": i.Index,
|
||||
"title": i.Title,
|
||||
"state": i.State,
|
||||
"html_url": i.HTMLURL,
|
||||
"user": userLogin(i.Poster),
|
||||
"comments": i.Comments,
|
||||
"created_at": i.Created,
|
||||
"updated_at": i.Updated,
|
||||
}
|
||||
if len(i.Labels) > 0 {
|
||||
m["labels"] = labelNames(i.Labels)
|
||||
}
|
||||
out = append(out, m)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func slimComment(c *gitea_sdk.Comment) map[string]any {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]any{
|
||||
"id": c.ID,
|
||||
"body": c.Body,
|
||||
"user": userLogin(c.Poster),
|
||||
"html_url": c.HTMLURL,
|
||||
"created_at": c.Created,
|
||||
"updated_at": c.Updated,
|
||||
}
|
||||
}
|
||||
|
||||
func slimComments(comments []*gitea_sdk.Comment) []map[string]any {
|
||||
out := make([]map[string]any, 0, len(comments))
|
||||
for _, c := range comments {
|
||||
out = append(out, slimComment(c))
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user