Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/a2a/__tests__/a2a-agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ describe('A2AAgent', () => {
await agent.invoke('Hello')
expect(mockGetAgentCard).toHaveBeenCalledOnce()
})

it('uses custom clientFactory when provided', async () => {
const customSendMessageStream = vi.fn().mockReturnValue(mockStream(createMockTaskResponse()))
const customGetAgentCard = vi.fn().mockResolvedValue(mockAgentCard)
const customCreateFromUrl = vi.fn().mockResolvedValue({
sendMessageStream: customSendMessageStream,
getAgentCard: customGetAgentCard,
})
const customFactory = { createFromUrl: customCreateFromUrl }

const agent = new A2AAgent({
url: 'http://localhost:9000',
clientFactory: customFactory as never,
})

await agent.invoke('Hello')

expect(customCreateFromUrl).toHaveBeenCalledWith('http://localhost:9000', undefined)
expect(customGetAgentCard).toHaveBeenCalledOnce()
expect(customSendMessageStream).toHaveBeenCalledOnce()
// Default mock should not have been called
expect(mockSendMessageStream).not.toHaveBeenCalled()
})
})

describe('stream', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/a2a/a2a-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import type { AgentCard, Part } from '@a2a-js/sdk'
import type { Client as A2AClientSdk } from '@a2a-js/sdk/client'
import type { Client as A2AClientSdk, ClientFactory as ClientFactoryType } from '@a2a-js/sdk/client'
import { ClientFactory } from '@a2a-js/sdk/client'
import type { InvokableAgent, InvokeArgs, InvokeOptions } from '../types/agent.js'
import { AgentResult } from '../types/agent.js'
Expand All @@ -31,6 +31,8 @@ export interface A2AAgentConfig {
name?: string
/** Optional description. If not provided, populated from the agent card after connection. */
description?: string
/** Optional custom A2A ClientFactory for authenticating requests (e.g. SigV4, bearer token). */
clientFactory?: ClientFactoryType
}

/**
Expand Down Expand Up @@ -170,7 +172,7 @@ export class A2AAgent implements InvokableAgent {

logExperimentalWarning()

const factory = new ClientFactory()
const factory = this._config.clientFactory ?? new ClientFactory()
const client = await factory.createFromUrl(this._config.url, this._config.agentCardPath)
this._agentCard = await client.getAgentCard()
if (this.name === undefined && this._agentCard?.name) {
Expand Down
Loading