-
Notifications
You must be signed in to change notification settings - Fork 6
Fix JavaScript SDK import path and authentication #17
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
alenkacz
wants to merge
2
commits into
redpanda-data:main
Choose a base branch
from
alenkacz:fix-javascript-sdk-import
base: main
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.
Conversation
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
The example was using incorrect import path:
- Old: import { A2AClient } from '@a2a-js/sdk'
- New: import { ClientFactory, ClientFactoryOptions } from '@a2a-js/sdk/client'
The A2AClient class is not exported from the main '@a2a-js/sdk' package,
it's available in the '/client' subpath export.
Also updated to use proper SDK authentication pattern with CallInterceptor
as recommended in the official SDK documentation.
Fixes the "A2AClient is not exported" error that users would encounter
when trying to run the example.
Co-Authored-By: Claude Sonnet 4.5 (1M context) <[email protected]>
a2a/javascript/README.md
Outdated
Comment on lines
61
to
93
| // Create A2A client from agent URL | ||
| const client = await A2AClient.createFromUrl(agentUrl, { | ||
| headers: { 'Authorization': `Bearer ${accessToken}` } | ||
| // Fetch agent card (public, no auth needed) | ||
| const cardResponse = await fetch(`${agentUrl}/.well-known/agent-card.json`); | ||
| const agentCard = await cardResponse.json(); | ||
|
|
||
| // Create A2A client with authentication | ||
| const options = ClientFactoryOptions.createFrom(ClientFactoryOptions.default, { | ||
| clientConfig: { | ||
| interceptors: [new AuthInterceptor(accessToken)], | ||
| }, | ||
| }); | ||
| const factory = new ClientFactory(options); | ||
| const client = await factory.createFromAgentCard(agentCard); |
Collaborator
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.
there is another thing we should do, you can patch the fetch API for the agent card to ensure the JWT token is provided properly, I did this for our implementation in Console UI, but maybe the API changed since few months ago.
const fetchWithCustomHeader: typeof fetch = async (url, init) => {
const headers = new Headers(init?.headers);
if (this.config.jwt) {
headers.set('Authorization', `Bearer ${this.config.jwt}`);
}
headers.set('X-Redpanda-Stream-Tokens', 'true');
const newInit = { ...init, headers };
return fetch(url, newInit);
};
const client = await createA2AClientWithFallback(this.modelId, { fetchImpl: fetchWithCustomHeader });then
async function createA2AClientWithFallback(agentUrl: string, options: A2AClientOptions): Promise<A2AClient> {
const urls = getAgentCardUrls({ agentUrl });
const errors: Error[] = [];
for (let i = 0; i < urls.length; i++) {
const url = urls[i];
const isLastUrl = i === urls.length - 1;
try {
const client = await A2AClient.fromCardUrl(url, options);
return client;
} catch (error) {
errors.push(error as Error);
console.log(`Failed to fetch agent card from ${url}, ${isLastUrl ? 'no more URLs to try' : 'trying next URL...'}`);
// Continue to next URL if not the last one
if (isLastUrl) {
break;
}
}
}then
export function getAgentCardUrls({ agentUrl }: { agentUrl: string }): string[] {
// Normalize URL by removing trailing slash
const normalizedUrl = agentUrl.endsWith('/') ? agentUrl.slice(0, -1) : agentUrl;
// If the URL already points to a specific file, use it as-is
const isFullPath =
normalizedUrl.includes('agent-card.json') ||
normalizedUrl.includes('agent.json') ||
normalizedUrl.includes('well-known');
if (isFullPath) {
return [normalizedUrl];
}
// Try agent-card.json first, fall back to agent.json
return [`${normalizedUrl}/.well-known/agent-card.json`, `${normalizedUrl}/.well-known/agent.json`];
}Implemented suggestions from PR review: - Added custom fetch wrapper that adds Authorization header - Added X-Redpanda-Stream-Tokens header for proper streaming - Implemented fallback to try both agent-card.json and agent.json paths - Used custom card resolver with SDK's createFromUrl method - Kept AuthInterceptor for API call authentication This ensures the JWT token is properly provided when fetching the agent card, matching the Console UI implementation pattern. Co-Authored-By: Claude Sonnet 4.5 (1M context) <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The example was using incorrect import path:
The A2AClient class is not exported from the main '@a2a-js/sdk' package, it's available in the '/client' subpath export.
Also updated to use proper SDK authentication pattern with CallInterceptor as recommended in the official SDK documentation.
Fixes the "A2AClient is not exported" error that users would encounter when trying to run the example.