mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-03-19 11:25:14 +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:
92
operation/actions/slim.go
Normal file
92
operation/actions/slim.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package actions
|
||||
|
||||
func pick(m map[string]any, keys ...string) map[string]any {
|
||||
out := make(map[string]any, len(keys))
|
||||
for _, k := range keys {
|
||||
if v, ok := m[k]; ok {
|
||||
out[k] = v
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func slimPaginated(raw any, itemFn func(map[string]any) map[string]any) any {
|
||||
m, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
return raw
|
||||
}
|
||||
result := make(map[string]any)
|
||||
if tc, ok := m["total_count"]; ok {
|
||||
result["total_count"] = tc
|
||||
}
|
||||
for key, val := range m {
|
||||
if key == "total_count" {
|
||||
continue
|
||||
}
|
||||
arr, ok := val.([]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
slimmed := make([]any, 0, len(arr))
|
||||
for _, item := range arr {
|
||||
if im, ok := item.(map[string]any); ok {
|
||||
slimmed = append(slimmed, itemFn(im))
|
||||
}
|
||||
}
|
||||
result[key] = slimmed
|
||||
break
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func slimRun(m map[string]any) map[string]any {
|
||||
return pick(m, "id", "name", "head_branch", "head_sha", "run_number",
|
||||
"event", "status", "conclusion", "workflow_id",
|
||||
"html_url", "created_at", "updated_at")
|
||||
}
|
||||
|
||||
func slimJob(m map[string]any) map[string]any {
|
||||
out := pick(m, "id", "run_id", "name", "workflow_name",
|
||||
"status", "conclusion", "html_url",
|
||||
"started_at", "completed_at")
|
||||
if steps, ok := m["steps"].([]any); ok {
|
||||
slim := make([]any, 0, len(steps))
|
||||
for _, s := range steps {
|
||||
if sm, ok := s.(map[string]any); ok {
|
||||
slim = append(slim, pick(sm, "name", "number", "status", "conclusion"))
|
||||
}
|
||||
}
|
||||
out["steps"] = slim
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func slimWorkflow(m map[string]any) map[string]any {
|
||||
return pick(m, "id", "name", "path", "state", "html_url", "created_at", "updated_at")
|
||||
}
|
||||
|
||||
func slimActionRun(raw any) any {
|
||||
if m, ok := raw.(map[string]any); ok {
|
||||
return slimRun(m)
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
func slimActionRuns(raw any) any {
|
||||
return slimPaginated(raw, slimRun)
|
||||
}
|
||||
|
||||
func slimActionJobs(raw any) any {
|
||||
return slimPaginated(raw, slimJob)
|
||||
}
|
||||
|
||||
func slimActionWorkflow(raw any) any {
|
||||
if m, ok := raw.(map[string]any); ok {
|
||||
return slimWorkflow(m)
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
func slimActionWorkflows(raw any) any {
|
||||
return slimPaginated(raw, slimWorkflow)
|
||||
}
|
||||
Reference in New Issue
Block a user