MCPcopy Create free account
hub / github.com/adcontextprotocol/adcp / getFormatsForAgent

Method getFormatsForAgent

server/src/formats.ts:17–70  ·  view source on GitHub ↗
(agent: Agent)

Source from the content-addressed store, hash-verified

15 private readonly CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes
16
17 async getFormatsForAgent(agent: Agent): Promise<AgentFormatsProfile> {
18 const cached = this.cache.get(agent.url);
19 if (cached && Date.now() - new Date(cached.last_fetched).getTime() < this.CACHE_TTL_MS) {
20 return cached;
21 }
22
23 let formats: FormatInfo[] = [];
24 let error: string | undefined;
25
26 try {
27 const agentConfig = {
28 id: agent.name,
29 name: agent.name,
30 agent_uri: agent.url,
31 protocol: (agent.protocol || "mcp") as "mcp" | "a2a",
32 };
33 const multiClient = new AdCPClient([agentConfig], { userAgent: AAO_UA_DISCOVERY });
34 const client = multiClient.agent(agent.name);
35 const result = await client.executeTask("list_creative_formats", {});
36
37 if (result.success && result.data) {
38 const response = result.data;
39
40 // Handle different response formats:
41 // 1. Array of formats directly
42 if (Array.isArray(response)) {
43 formats = response.map(this.normalizeFormat);
44 }
45 // 2. Object with formats array
46 else if (response?.formats && Array.isArray(response.formats)) {
47 formats = response.formats.map(this.normalizeFormat);
48 }
49 // 3. Single format object
50 else if (response && typeof response === "object") {
51 formats = [this.normalizeFormat(response)];
52 }
53 } else if (!result.success) {
54 error = `Agent returned error: ${result.error || "Unknown error"}`;
55 }
56 } catch (toolError: any) {
57 error = `Agent does not support list_creative_formats: ${toolError.message}`;
58 }
59
60 const profile: AgentFormatsProfile = {
61 agent_url: agent.url,
62 protocol: agent.protocol || "mcp",
63 formats,
64 last_fetched: new Date().toISOString(),
65 error,
66 };
67
68 this.cache.set(agent.url, profile);
69 return profile;
70 }
71
72 private normalizeFormat(format: any): FormatInfo {
73 // Handle string format (just a name)

Callers 4

fetchStatsMethod · 0.80
formats.test.tsFile · 0.80

Calls 5

normalizeFormatMethod · 0.95
nowMethod · 0.80
agentMethod · 0.80
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected