-
Notifications
You must be signed in to change notification settings - Fork 70
feat(mcp): build flagset from mcp input schema to parse args #1215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
d1a299e
chore: update vulnerable packages (#1229)
keegancsmith 2e48170
remove tool metion from help text (#1230)
burmudar 4de2c09
add script to dump mcp tool list
burmudar bd20984
parse mcp tool json
burmudar 6d269dd
fix parsing for when items: true
burmudar 4cf4889
use lib/errors
burmudar e479290
temporarily ignore embedded json
burmudar 1164172
move mcp files to internal/mcp
burmudar 5fab60f
unexport and remove unused structs
burmudar 72974c4
rename MCPToolDef to ToolDef and move around structs
burmudar 58bbc3c
rename parser + method to decoder + decode*
burmudar 505a7b0
simplify types fold: Schema into SchemaObject
burmudar d01f5ea
add mcp list-tools to list available tool calls
burmudar c4306cb
set variable name for embedded json
burmudar bfdfe06
fix usage of fmt.Errorf
burmudar 6297121
fix test
burmudar bc4a87d
fix lint error
burmudar 6603541
make default LoadToolDefinitions and unexported loadToolDefinitions
burmudar 14b9832
use internal/mcp
burmudar 07f9d05
fixup after refactor
burmudar 312a810
improve list-tools output
burmudar 89d742e
change name to RawName and have method for Name() that is normalized
burmudar cf08d88
remove Name() method and use method name field
burmudar 3cb9b00
fix test
burmudar 5840064
fixup
burmudar 6160b49
fixup
burmudar 49af84d
build flagset from inputschema to parse args
burmudar 169b5e6
initial handleMcpTool method
burmudar 9f4c7e3
export LoadToolDefinitions and add LoadDefaultToolDefinitions
burmudar 45368db
move mcp args to internal/mcp
burmudar b236f16
guard against nil tool definitions when building flagsets
burmudar 1cc1c27
use github.com/sourcegraph/sourcegraphh/lib/errors
burmudar 8d0b3f6
fixup old Kind usage
burmudar 3815a50
add todo
burmudar 4839034
feat(mcp): do mcp tool call from cli args (#1216)
burmudar 700a6cb
add todo
burmudar 897aae6
Merge branch 'wb/mcp-list-tools' into wb/mcp-flagset-inputschema
burmudar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package mcp | ||
|
|
||
| import ( | ||
| "flag" | ||
| "fmt" | ||
| "reflect" | ||
| "strings" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/lib/errors" | ||
| ) | ||
|
|
||
| var _ flag.Value = (*strSliceFlag)(nil) | ||
|
|
||
| type strSliceFlag struct { | ||
| vals []string | ||
| } | ||
|
|
||
| func (s *strSliceFlag) Set(v string) error { | ||
| s.vals = append(s.vals, v) | ||
| return nil | ||
| } | ||
|
|
||
| func (s *strSliceFlag) String() string { | ||
| return strings.Join(s.vals, ",") | ||
| } | ||
|
|
||
| func DerefFlagValues(vars map[string]any) { | ||
| for k, v := range vars { | ||
| rfl := reflect.ValueOf(v) | ||
| if rfl.Kind() == reflect.Pointer { | ||
| vv := rfl.Elem().Interface() | ||
| if slice, ok := vv.(strSliceFlag); ok { | ||
| vv = slice.vals | ||
| } | ||
| if isNil(vv) { | ||
| delete(vars, k) | ||
| } else { | ||
| vars[k] = vv | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func isNil(v any) bool { | ||
| if v == nil { | ||
| return true | ||
| } | ||
| rv := reflect.ValueOf(v) | ||
| switch rv.Kind() { | ||
| case reflect.Slice, reflect.Map, reflect.Pointer, reflect.Interface: | ||
| return rv.IsNil() | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| func BuildArgFlagSet(tool *ToolDef) (*flag.FlagSet, map[string]any, error) { | ||
| if tool == nil { | ||
| return nil, nil, errors.New("cannot build flagset on nil Tool Definition") | ||
| } | ||
| fs := flag.NewFlagSet(tool.Name, flag.ContinueOnError) | ||
| flagVars := map[string]any{} | ||
|
|
||
| for name, pVal := range tool.InputSchema.Properties { | ||
| switch pv := pVal.(type) { | ||
| case *SchemaPrimitive: | ||
| switch pv.Type { | ||
| case "integer": | ||
| dst := fs.Int(name, 0, pv.Description) | ||
| flagVars[name] = dst | ||
|
|
||
| case "boolean": | ||
| dst := fs.Bool(name, false, pv.Description) | ||
| flagVars[name] = dst | ||
| case "string": | ||
| dst := fs.String(name, "", pv.Description) | ||
| flagVars[name] = dst | ||
| default: | ||
| return nil, nil, fmt.Errorf("unknown schema primitive kind %q", pv.Type) | ||
|
|
||
| } | ||
| case *SchemaArray: | ||
| strSlice := new(strSliceFlag) | ||
| fs.Var(strSlice, name, pv.Description) | ||
| flagVars[name] = strSlice | ||
| case *SchemaObject: | ||
| // TODO(burmudar): we can support SchemaObject as part of stdin echo '{ stuff }' | sg mcp commit-search | ||
burmudar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // not supported yet | ||
| // Also support sg mcp commit-search --json '{ stuff }' | ||
| } | ||
| } | ||
|
|
||
| return fs, flagVars, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have nay uses of that in our tools?