Skip to content

Conversation

@jiapingzeng
Copy link
Contributor

@jiapingzeng jiapingzeng commented Oct 29, 2025

Description

Create MCP client on each execute request + forward headers to MCP connector

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Accepted headers:

x-amzn-fas-accesskey, x-amzn-fas-secretkey, x-amzn-fas-sessiontoken, x-amzn-datasources

Sample request:

POST {{endpoint}}/_plugins/_ml/agents/{{agent_id}}/_execute
--header 'x-amzn-fas-accesskey: example' \
--header 'x-amzn-fas-secretkey: example' \
--header 'x-amzn-fas-sessiontoken: example' \
--header 'x-amzn-datasources: https://example.aos.us-east-1.on.aws' \
--header 'Content-Type: application/json'
{
    "parameters": {
        "question": "list indices"
    },
    "verbose": "true"
}

Response:

{
    "inference_results": [
        {
            "output": [
                {
                    "name": "memory_id",
                    "result": "F7vALZoBCqjo4z06S5j0"
                },
                {
                    "name": "parent_interaction_id",
                    "result": "GbvALZoBCqjo4z06TZiW"
                },
                {
                    "name": "response",
                    "dataAsMap": {
                        "response": "Here are the indices in your OpenSearch cluster:\n\n## User Indices\n1. **tmdb** - 8,516 documents, green health\n...",
                        "additional_info": {}
                    }
                }
            ]
        }
    ]
}

Copy link
Collaborator

@Zhangxunmt Zhangxunmt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spotless needs to apply

@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 29, 2025 19:48 — with GitHub Actions Waiting
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 29, 2025 19:48 — with GitHub Actions Waiting
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 29, 2025 19:48 — with GitHub Actions Waiting
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 29, 2025 19:48 — with GitHub Actions Waiting
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 30, 2025 01:54 — with GitHub Actions Waiting
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 30, 2025 01:54 — with GitHub Actions Waiting
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 30, 2025 01:54 — with GitHub Actions Waiting
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 30, 2025 01:54 — with GitHub Actions Waiting
Signed-off-by: Jiaping Zeng <[email protected]>
Signed-off-by: Jiaping Zeng <[email protected]>
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 30, 2025 02:15 — with GitHub Actions Waiting
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 30, 2025 02:15 — with GitHub Actions Waiting
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 30, 2025 02:15 — with GitHub Actions Waiting
@jiapingzeng jiapingzeng requested a deployment to ml-commons-cicd-env-require-approval October 30, 2025 02:15 — with GitHub Actions Waiting
return Collections.emptyMap();
}

String headersJson = parameters.get(CommonValue.MCP_REQUEST_HEADERS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the expectation that header contains a json? this seems a anti pattern.

Duration connectionTimeout = Duration.ofSeconds(super.getConnectorClientConfig().getConnectionTimeout());
Duration readTimeout = Duration.ofSeconds(super.getConnectorClientConfig().getReadTimeout());

Map<String, String> mergedHeaders = new HashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a mergedHeaders might be unnecessary. Request header could be directly added to builder while creating headerConfig

Comment on lines +81 to +86
if (connector.getDecryptedHeaders() != null) {
mergedHeaders.putAll(connector.getDecryptedHeaders());
}
if (requestHeaders != null) {
mergedHeaders.putAll(requestHeaders);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

* @param request RestRequest containing the MCP headers
* @param agentInput AgentMLInput to add the headers to
*/
public static void addMcpRequestHeaders(RestRequest request, AgentMLInput agentInput) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add unit test.

String headersJson = gson.toJson(headers);
RemoteInferenceInputDataSet inputDataSet = (RemoteInferenceInputDataSet) agentInput.getInputDataset();
if (inputDataSet != null && inputDataSet.getParameters() != null) {
inputDataSet.getParameters().put(CommonValue.MCP_REQUEST_HEADERS, headersJson);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we are overriding parameters to also include headers. Can we rather create a new header field to RemoteInferenceInputDataSet for headers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants