40 lines
1.3 KiB
Go
40 lines
1.3 KiB
Go
package tools
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"code.nochebuena.dev/einherjar/mcp/internal/index"
|
||
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||
|
|
)
|
||
|
|
|
||
|
|
type getExampleInput struct {
|
||
|
|
Module string `json:"module" jsonschema:"the module name, e.g. core"`
|
||
|
|
Topic string `json:"topic,omitempty" jsonschema:"optional substring matched against the example title (case-insensitive)"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type getExampleOutput struct {
|
||
|
|
Examples []index.Example `json:"examples"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func registerGetExample(s *mcp.Server, idx *index.Index) {
|
||
|
|
mcp.AddTool(s, &mcp.Tool{
|
||
|
|
Name: "get_example",
|
||
|
|
Description: "Return canonical usage examples for a module, extracted from its README. Filter by topic substring (e.g. 'Logger', 'Launcher') to narrow the result.",
|
||
|
|
}, func(ctx context.Context, req *mcp.CallToolRequest, args getExampleInput) (*mcp.CallToolResult, getExampleOutput, error) {
|
||
|
|
m := idx.FindModule(args.Module)
|
||
|
|
if m == nil {
|
||
|
|
return errorResult("module not found: " + args.Module), getExampleOutput{}, nil
|
||
|
|
}
|
||
|
|
needle := strings.ToLower(args.Topic)
|
||
|
|
out := getExampleOutput{Examples: []index.Example{}}
|
||
|
|
for _, ex := range m.Examples {
|
||
|
|
if needle != "" && !strings.Contains(strings.ToLower(ex.Title), needle) {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
out.Examples = append(out.Examples, ex)
|
||
|
|
}
|
||
|
|
return jsonText(out), out, nil
|
||
|
|
})
|
||
|
|
}
|