-
Notifications
You must be signed in to change notification settings - Fork 255
feat(router): expose prompt-to-query via MCP #3158
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
fiam
wants to merge
6
commits into
wilson/cosmo-335-cosmo-cloud-prompt-to-query-via-mcp-product
from
alberto/router-575-router-mcp-query-generation-support
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b362600
refactor(router): centralize control plane transport
fiam 858f622
feat(router): add AI query generation client
fiam 738985b
feat(router): configure query generation scope
fiam 3bff796
feat(router): add generate_query MCP tool
fiam bfaf144
feat(router): gate query generation by token feature
fiam 20122ad
docs(router): document query generation tool
fiam 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
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,58 @@ | ||
| package controlplane | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/hashicorp/go-retryablehttp" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| const ( | ||
| retryWaitMax = 15 * time.Second | ||
| retryMax = 3 | ||
| ) | ||
|
|
||
| type bearerAuthTransport struct { | ||
| token string | ||
| next http.RoundTripper | ||
| } | ||
|
|
||
| // NewTransport creates an authenticated, retrying transport for control plane requests. | ||
| func NewTransport(token string, logger *zap.Logger) (http.RoundTripper, error) { | ||
| if token == "" { | ||
| return nil, fmt.Errorf("graph api token is required for controlplane requests") | ||
| } | ||
|
|
||
| if logger == nil { | ||
| logger = zap.NewNop() | ||
| } | ||
|
|
||
| retryClient := retryablehttp.NewClient() | ||
| retryClient.RetryWaitMax = retryWaitMax | ||
| retryClient.RetryMax = retryMax | ||
| retryClient.Backoff = retryablehttp.DefaultBackoff | ||
| retryClient.Logger = nil | ||
| retryClient.RequestLogHook = func(_ retryablehttp.Logger, _ *http.Request, retry int) { | ||
| if retry > 0 { | ||
| logger.Info("Retry controlplane request", zap.Int("retry", retry)) | ||
| } | ||
| } | ||
|
|
||
| return newBearerAuthTransport(token, retryClient.StandardClient().Transport), nil | ||
| } | ||
|
|
||
| func newBearerAuthTransport(token string, next http.RoundTripper) http.RoundTripper { | ||
| return &bearerAuthTransport{ | ||
| token: token, | ||
| next: next, | ||
| } | ||
| } | ||
|
|
||
| func (t *bearerAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| req = req.Clone(req.Context()) | ||
| req.Header.Set("Authorization", "Bearer "+t.token) | ||
|
|
||
| return t.next.RoundTrip(req) | ||
| } |
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,56 @@ | ||
| package prompttoquery | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "connectrpc.com/connect" | ||
| aiv1 "github.com/wundergraph/cosmo/router/gen/proto/wg/cosmo/ai/v1" | ||
| "github.com/wundergraph/cosmo/router/gen/proto/wg/cosmo/ai/v1/aiv1connect" | ||
| brotli "go.withmatt.com/connect-brotli" | ||
| ) | ||
|
|
||
| const clientTimeout = 15 * time.Second | ||
|
|
||
| // Client generates GraphQL operations through the control plane. | ||
| type Client struct { | ||
| aiServiceClient aiv1connect.AIServiceClient | ||
| } | ||
|
|
||
| func New(endpoint string, transport http.RoundTripper) (*Client, error) { | ||
| if endpoint == "" { | ||
| return nil, fmt.Errorf("controlplane endpoint is required for prompt to query") | ||
| } | ||
|
|
||
| if transport == nil { | ||
| return nil, fmt.Errorf("controlplane transport is required for prompt to query") | ||
| } | ||
|
|
||
| httpClient := &http.Client{ | ||
| Transport: transport, | ||
| Timeout: clientTimeout, | ||
| } | ||
|
|
||
| aiServiceClient := aiv1connect.NewAIServiceClient(httpClient, endpoint, | ||
| brotli.WithCompression(), | ||
| connect.WithSendCompression(brotli.Name), | ||
| ) | ||
|
|
||
| return &Client{aiServiceClient: aiServiceClient}, nil | ||
| } | ||
|
|
||
| func (c *Client) GenerateQuery(ctx context.Context, schemaVersionID, prompt string) (*aiv1.GenerateQueryResponse, error) { | ||
| req := connect.NewRequest(&aiv1.GenerateQueryRequest{ | ||
| Version: schemaVersionID, | ||
| Prompt: prompt, | ||
| }) | ||
|
|
||
| resp, err := c.aiServiceClient.GenerateQuery(ctx, req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return resp.Msg, nil | ||
| } |
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
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
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.
Unsure about this, see my PM