[Feature]: Extend the v2 Streamable HTTP example with optional Bearer auth and bounded research usage
Is your feature request related to a problem? Please describe.
The current v2 McpStreamableHttpExample accepts MCP_HTTP_URL and an optional X-API-Key, while the v2 tool guide already shows an Authorization: Bearer header. Users of Bearer-authenticated research servers still need to adapt the runnable example, choose a small tool set, and arrange client cleanup. The example does not currently close its MCP client when its interactive loop exits.
Describe the solution you'd like
Would a small example/documentation contribution in the following scope fit the project?
- Extend
agentscope-examples/documentation/src/main/java/io/agentscope/examples/documentation2/mcp/McpStreamableHttpExample.java: retain the local default and existing X-API-Key option, add an explicit optional Bearer token read from the environment, document mutually exclusive auth choices, and close the client on normal exit or failure. - Add a short worked research configuration to
docs/v2/{en,zh}/docs/building-blocks/tool.md, using the existing MCP registration/filtering path to expose at most three explicitly selected tools. Keep returned text/results intact; do not invent citation or date fields. - Use a loopback MCP server and synthetic credentials for tests of initialization, tool listing, filtered registration, a tool call through the toolkit, invalid credentials, and cleanup. Keep model responses stubbed and bound the number of calls in the example test.
Baizhi Cloud Agent Toolkit could be an explicitly optional worked instance, with its endpoint supplied by the user; this would add no default provider, endpoint, or core dependency. I can prepare the focused change and respond to review feedback if this scope is useful.
Describe alternatives you've considered
The existing generic MCP client already provides the needed transport and static headers. A dedicated provider API would duplicate that capability. This proposal also leaves the native Harness search-provider work in #3241 / #3242 unchanged.
Additional context
Disclosure: I am helping promote Baizhi Cloud Agent Toolkit integrations. Its MCP endpoint is externally hosted and requires the user's own key; service usage and model calls may incur charges. Tool arguments and request metadata are sent to the selected service. The open-source client example would not make the hosted service itself open source. Public integration documentation: https://github.com/chaitin/baizhi-agent-toolkit .
This proposal is based on source inspection at ed77530a4139f864fc1c730d10c0e9f57707aba7 (v2 examples, not the legacy v1 guide). No production MCP or model calls have been made. Local validation should also cover the SSE-disabled response case reported in #2003 and empty 202 responses discussed in #2959; the current BOM already uses MCP SDK 0.17.2, so this is not a request to repeat that dependency bump. This proposal does not establish live-service compatibility. If the worked service example is outside the desired scope, the auth/cleanup clarification can remain provider-neutral.
Issue Evaluation
Category: enhancement | Status: Evaluated — accurate premise, small change, but scope needs a maintainer call (verified against main @ ed77530a4)
Claims verified against the current code.
MCP_HTTP_URLis read with a localhost default andX-API-Keyis the only optional credential —agentscope-examples/documentation/src/main/java/io/agentscope/examples/documentation2/mcp/McpStreamableHttpExample.java:69-73and:82-84.- The missing-cleanup claim is correct: there is no
close()and no try-with-resources anywhere in that file;maindeclaresthrows Exceptionwith nofinally. The primitive already exists and is unused —McpClientWrapper implements AutoCloseable(agentscope-core/src/main/java/io/agentscope/core/tool/mcp/McpClientWrapper.java:40,close()at:130) andMcpAsyncClientWrapper.close()blocks oncloseGracefully()(McpAsyncClientWrapper.java:182-194). - The same omission exists in the sibling examples
McpSseExample.java(138 lines) andMcpStdioExample.java(130 lines), so this is a pattern across all three MCP examples, not one file. - The docs inconsistency is real:
docs/v2/en/docs/building-blocks/tool.md:356anddocs/v2/zh/docs/building-blocks/tool.md:356both use.header("Authorization", "Bearer xxx"), while the example usesX-API-Key. - A dedicated provider API is indeed unnecessary:
McpClientBuilder.header(String,String)(:279) andheaders(Map)(:292) already feedHttpTransportConfig(:666-672), used byStreamableHttpTransportConfig(:812). - No dependency bump is implied:
agentscope-dependencies-bom/pom.xml:83pins<mcp.version>0.17.2</mcp.version>.
Feasibility: feasible and small. Bearer-from-env (~5-7 lines), explicit X-API-Key/Bearer mutual exclusion (~4-6 lines) and deterministic client cleanup (~6-12 lines) come to roughly 20-30 lines in a 139-line file, purely additive with no source/binary compatibility impact.
Points worth settling before any PR:
- The referenced doc snippet does not compile against today's API.
tool.md:356usesMcpClientBuilder.streamableHttp().name(..).url(..).build(); the real API isMcpClientBuilder.create(String)(:118),streamableHttpTransport(String)(:192), andbuildAsync()/buildSync()(:469/:512). If this issue is picked up, repairing that snippet in bothenandzhshould be part of it — otherwise the example and the guide stay inconsistent in a second way. - Mutual-exclusion validation has no home in the SDK.
header()/headers()write into aHashMap, so a duplicate credential header silently overwrites. Validating it means either example-side checks (as proposed here) or a new builder feature — a noticeably larger change than the issue implies. - Env-var naming needs a doc contract. Only
MCP_HTTP_URL/MCP_SSE_URL/MCP_HTTP_API_KEY/MCP_SSE_TOKENare documented today (agentscope-examples/README.md:215-218), so a new Bearer variable has to land in the README plus the example javadoc (McpStreamableHttpExample.java:37-41) at the same time.docs/v2/zh/integration/model/anthropic.md:55-57is the closest existing precedent for two mutually exclusive credentials. - The proposed test plan is net-new infrastructure. No MCP server is ever started in this repo's tests (
McpServer.builder/McpSyncServer/McpAsyncServerhave zero hits); MCP client tests are Mockito-based (agentscope-core/src/test/java/io/agentscope/core/tool/mcp/), and MCP tool allow/deny filtering lives in the harness (agentscope-harness/.../tools/ToolFilter.java,McpServerConfig.enableTools/disableTools), not in core or the examples. A loopback MCP server test would need new scaffolding (MockWebServer is already a global test dependency,pom.xml:127-131). Thedocumentationmodule has nosrc/testtoday — tests could be added without pom edits since junit/mockito/surefire are inherited from the root pom — and CI is dual-OS (ubuntu+windows) with a license-header check on every new file. - Vendor-specific worked example. Adding a named third-party service (Baizhi Cloud Agent Toolkit) to project docs is a product/scope decision for maintainers rather than a code question; the author already frames it as optional and provider-neutral fallback, which is the right posture. The issue's charge/egress disclosure (user-supplied endpoint, user's own key, request metadata leaving the process) is worth keeping in any merged version.
Recommendation: the provider-neutral part (Bearer option, explicit auth precedence, guaranteed client cleanup, plus the stale doc snippet fix) is a low-risk contribution worth welcoming; the vendor-specific worked example and the loopback-MCP test harness should be confirmed by maintainers first. The cleanup fix is likely more valuable applied to all three MCP examples than to the streamable-HTTP one alone.
Marking this enhancement + area/examples + area/docs for maintainer triage. No bot-generated PR in this round: the request is a scoped feature proposal that explicitly asks whether the scope fits the project, and that answer belongs to maintainers before code is written.
Automated evaluation by github-manager-bot
I'd like to take this one. Plan per the evaluation:
- Add optional Bearer auth from an env var to
McpStreamableHttpExample(and the same deterministic client cleanup with try-with-resources / finally to all three MCP examples: streamable-http, SSE, stdio). - X-API-Key and Bearer stay mutually exclusive, validated example-side only (no SDK change).
- Fix the
docs/v2/en|zh/docs/building-blocks/tool.mdsnippet to use the real API (McpClientBuilder.create(..).streamableHttpTransport(..)..buildAsync()).
One naming question: for the Bearer env var, is MCP_HTTP_BEARER_TOKEN (and MCP_SSE_BEARER_TOKEN for the SSE example) acceptable, consistent with the existing MCP_HTTP_API_KEY / MCP_SSE_TOKEN naming? Happy to adjust if you prefer something else — will wait briefly for a reply, then proceed with these names if there's no objection.
PR submitted: #3268 — Bearer env var (MCP_HTTP_BEARER_TOKEN, mutually exclusive with MCP_HTTP_API_KEY), try-with-resources cleanup in all three MCP examples, and the doc-snippet API fix in en/zh tool.md. Proceeded with the proposed naming since there was no objection; easy to rename if maintainers prefer another convention.