-
Notifications
You must be signed in to change notification settings - Fork 70
feat(mcp): do mcp tool call from cli args #1216
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
Open
burmudar
wants to merge
7
commits into
wb/mcp-flagset-inputschema
Choose a base branch
from
wb/mcp-http-tool-call
base: wb/mcp-flagset-inputschema
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−18
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
696c8bc
make json rpc 2.0 call based on cli args
burmudar f6e1f59
move tool request to mcp_request.go
burmudar 4e0b03e
print tool response according to output schema
burmudar ef83d36
print structuredContent from JSON RPC response
burmudar 84ddb92
only register mcp command if SRC_EXPERIMENT_MCP=true
burmudar 8353159
rename ParseToolResponse to DecodeToolResponse
burmudar 81a073e
fixup types
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,11 +32,28 @@ func DerefFlagValues(vars map[string]any) { | |
| if slice, ok := vv.(strSliceFlag); ok { | ||
| vv = slice.vals | ||
| } | ||
| vars[k] = vv | ||
| if isNil(vv) { | ||
| delete(vars, k) | ||
| } else { | ||
| vars[k] = vv | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func isNil(v any) bool { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fun times |
||
| 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") | ||
|
|
||
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,93 @@ | ||
| package mcp | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| "github.com/sourcegraph/src-cli/internal/api" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/lib/errors" | ||
| ) | ||
|
|
||
| const McpURLPath = ".api/mcp/v1" | ||
|
|
||
| func DoToolRequest(ctx context.Context, client api.Client, tool *ToolDef, vars map[string]any) (*http.Response, error) { | ||
| jsonRPC := struct { | ||
| Version string `json:"jsonrpc"` | ||
| ID int `json:"id"` | ||
| Method string `json:"method"` | ||
| Params any `json:"params"` | ||
| }{ | ||
| Version: "2.0", | ||
| ID: 1, | ||
| Method: "tools/call", | ||
| Params: struct { | ||
| Name string `json:"name"` | ||
| Arguments map[string]any `json:"arguments"` | ||
| }{ | ||
| Name: tool.RawName, | ||
| Arguments: vars, | ||
| }, | ||
| } | ||
|
|
||
| buf := bytes.NewBuffer(nil) | ||
| data, err := json.Marshal(jsonRPC) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| buf.Write(data) | ||
|
|
||
| req, err := client.NewHTTPRequest(ctx, http.MethodPost, McpURLPath, buf) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| req.Header.Add("Content-Type", "application/json") | ||
| req.Header.Add("Accept", "*/*") | ||
|
|
||
| return client.Do(req) | ||
| } | ||
|
|
||
| func DecodeToolResponse(resp *http.Response) (map[string]json.RawMessage, error) { | ||
| data, err := readSSEResponseData(resp) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought our server was just http not sse. But then again we use some framework that I guess decides for us? |
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if data == nil { | ||
| return map[string]json.RawMessage{}, nil | ||
| } | ||
|
|
||
| jsonRPCResp := struct { | ||
| Version string `json:"jsonrpc"` | ||
| ID int `json:"id"` | ||
| Result struct { | ||
| Content []json.RawMessage `json:"content"` | ||
| StructuredContent map[string]json.RawMessage `json:"structuredContent"` | ||
| } `json:"result"` | ||
| }{} | ||
| if err := json.Unmarshal(data, &jsonRPCResp); err != nil { | ||
| return nil, errors.Wrapf(err, "failed to unmarshal MCP JSON-RPC response") | ||
| } | ||
|
|
||
| return jsonRPCResp.Result.StructuredContent, nil | ||
| } | ||
| func readSSEResponseData(resp *http.Response) ([]byte, error) { | ||
| data, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| // The response is an SSE reponse | ||
| // event: | ||
| // data: | ||
| lines := bytes.SplitSeq(data, []byte("\n")) | ||
| for line := range lines { | ||
| if jsonData, ok := bytes.CutPrefix(line, []byte("data: ")); ok { | ||
| return jsonData, nil | ||
| } | ||
| } | ||
| return nil, errors.New("no data found in SSE response") | ||
|
|
||
| } | ||
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.
I mentioned in slack just not advertising this command. This also works, but would be nice to more easily experiment without the envvar.