@@ -4,7 +4,6 @@ package mcp
44import (
55 "context"
66 "fmt"
7- "sort"
87 "strings"
98
109 "github.com/mark3labs/mcp-go/mcp"
@@ -30,6 +29,7 @@ type fileCount struct {
3029// commCount holds aggregated membership counts per community.
3130// @intent transport GROUP BY community_id results into MCP response shaping.
3231type commCount struct {
32+ Label string
3333 CommunityID uint
3434 Count int
3535}
@@ -44,6 +44,7 @@ type minimalContextCommInfo struct {
4444// flowCount holds aggregated membership counts per flow.
4545// @intent transport GROUP BY flow_id results into MCP response shaping.
4646type flowCount struct {
47+ Name string
4748 FlowID uint
4849 Count int
4950}
@@ -175,60 +176,37 @@ func (h *handlers) getMinimalContext(ctx context.Context, request mcp.CallToolRe
175176 Joins ("JOIN communities ON communities.id = community_memberships.community_id" ).
176177 Where ("communities.namespace = ?" , ns )
177178 if err := commCountQ .
178- Select ("community_id, COUNT(*) as count" ).
179+ Select ("community_id, communities.label as label, COUNT(*) as count" ).
179180 Group ("community_id" ).
181+ Group ("communities.label" ).
182+ Order ("count DESC" ).
183+ Order ("community_id ASC" ).
184+ Limit (3 ).
180185 Scan (& ccRows ).Error ; err != nil {
181186 return "" , trace .Wrap (err , "group community memberships" )
182187 }
183- ccMap := make (map [uint ]int , len (ccRows ))
184- for _ , r := range ccRows {
185- ccMap [r .CommunityID ] = r .Count
186- }
187-
188- var communities []model.Community
189- communityQ := h .deps .DB .WithContext (ctx ).Where ("namespace = ?" , ns )
190- if err := communityQ .Find (& communities ).Error ; err != nil {
191- return "" , trace .Wrap (err , "find communities" )
192- }
193- commInfos := make ([]minimalContextCommInfo , len (communities ))
194- for i , c := range communities {
195- commInfos [i ] = minimalContextCommInfo {Label : c .Label , NodeCount : ccMap [c .ID ]}
196- }
197- sort .Slice (commInfos , func (i , j int ) bool {
198- return commInfos [i ].NodeCount > commInfos [j ].NodeCount
199- })
200- if len (commInfos ) > 3 {
201- commInfos = commInfos [:3 ]
188+ commInfos := make ([]minimalContextCommInfo , len (ccRows ))
189+ for i , r := range ccRows {
190+ commInfos [i ] = minimalContextCommInfo {Label : r .Label , NodeCount : r .Count }
202191 }
203192 var fcRows []flowCount
204193 flowCountQ := h .deps .DB .WithContext (ctx ).
205194 Model (& model.FlowMembership {}).
206- Where ("namespace = ?" , ns )
195+ Joins ("JOIN flows ON flows.id = flow_memberships.flow_id" ).
196+ Where ("flow_memberships.namespace = ?" , ns )
207197 if err := flowCountQ .
208- Select ("flow_id, COUNT(*) as count" ).
198+ Select ("flow_id, flows.name as name, COUNT(*) as count" ).
209199 Group ("flow_id" ).
200+ Group ("flows.name" ).
201+ Order ("count DESC" ).
202+ Order ("flow_id ASC" ).
203+ Limit (3 ).
210204 Scan (& fcRows ).Error ; err != nil {
211205 return "" , trace .Wrap (err , "group flow memberships" )
212206 }
213- fcMap := make (map [uint ]int , len (fcRows ))
214- for _ , r := range fcRows {
215- fcMap [r .FlowID ] = r .Count
216- }
217-
218- var flowList []model.Flow
219- flowQ := h .deps .DB .WithContext (ctx ).Where ("namespace = ?" , ns )
220- if err := flowQ .Find (& flowList ).Error ; err != nil {
221- return "" , trace .Wrap (err , "find flows" )
222- }
223- flowInfos := make ([]minimalContextFlowInfo , len (flowList ))
224- for i , f := range flowList {
225- flowInfos [i ] = minimalContextFlowInfo {Name : f .Name , NodeCount : fcMap [f .ID ]}
226- }
227- sort .Slice (flowInfos , func (i , j int ) bool {
228- return flowInfos [i ].NodeCount > flowInfos [j ].NodeCount
229- })
230- if len (flowInfos ) > 3 {
231- flowInfos = flowInfos [:3 ]
207+ flowInfos := make ([]minimalContextFlowInfo , len (fcRows ))
208+ for i , r := range fcRows {
209+ flowInfos [i ] = minimalContextFlowInfo {Name : r .Name , NodeCount : r .Count }
232210 }
233211
234212 suggestedTools := suggestTools (task )
0 commit comments