1- import type { InferUITool } from '@mastra/core/tools'
21import { createTool } from '@mastra/core/tools'
2+ import type { InferUITool } from '@mastra/core/tools'
33import { trace } from '@opentelemetry/api'
44import { z } from 'zod'
55import { log } from '../config/logger'
66
7+ import type { RequestContext } from '@mastra/core/request-context'
8+
79/**
810 * Alpha Vantage Tools
911 *
@@ -14,6 +16,13 @@ import { log } from '../config/logger'
1416 * Requires ALPHA_VANTAGE_API_KEY environment variable
1517 */
1618
19+ /**
20+ * Base Request Context for Alpha Vantage
21+ */
22+ export interface AlphaVantageRequestContext extends RequestContext {
23+ apiKey ?: string
24+ }
25+
1726// In-memory counter to track tool calls per request
1827// Add this line at the beginning of each tool's execute function to track usage:
1928// toolCallCounters.set('tool-id', (toolCallCounters.get('tool-id') ?? 0) + 1)
@@ -83,6 +92,7 @@ export const alphaVantageCryptoTool = createTool({
8392 . optional ( ) ,
8493 } ) ,
8594 execute : async ( inputData , context ) => {
95+ const requestContext = context ?. requestContext as AlphaVantageRequestContext
8696 const span = trace
8797 . getTracer ( 'alpha-vantage-crypto-tool' , '1.0.0' )
8898 . startSpan ( 'alpha-vantage-crypto' , {
@@ -103,7 +113,7 @@ export const alphaVantageCryptoTool = createTool({
103113 } ,
104114 id : 'alpha-vantage-crypto' ,
105115 } )
106- const apiKey = process . env . ALPHA_VANTAGE_API_KEY
116+ const apiKey = requestContext ?. apiKey ?? process . env . ALPHA_VANTAGE_API_KEY
107117
108118 if ( typeof apiKey !== 'string' || apiKey . trim ( ) === '' ) {
109119 await context ?. writer ?. custom ( {
@@ -313,7 +323,7 @@ export const alphaVantageCryptoTool = createTool({
313323 } )
314324 } ,
315325 onOutput : ( { output, toolCallId, toolName, abortSignal } ) => {
316- const dataKeys = output . data ? Object . keys ( output . data ) . length : 0
326+ const dataKeys = ( output . data !== null && typeof output . data === 'object' ) ? Object . keys ( output . data as object ) . length : 0
317327 log . info ( 'Alpha Vantage crypto completed' , {
318328 toolCallId,
319329 toolName,
@@ -412,6 +422,7 @@ export const alphaVantageStockTool = createTool({
412422 } ) ,
413423
414424 execute : async ( inputData , context ) => {
425+ const requestContext = context ?. requestContext as AlphaVantageRequestContext
415426 const span = trace
416427 . getTracer ( 'alpha-vantage-stock-tool' , '1.0.0' )
417428 . startSpan ( 'alpha-vantage-stock' , {
@@ -431,7 +442,7 @@ export const alphaVantageStockTool = createTool({
431442 } ,
432443 id : 'alpha-vantage-stock' ,
433444 } )
434- const apiKey = process . env . ALPHA_VANTAGE_API_KEY
445+ const apiKey = requestContext ?. apiKey ?? process . env . ALPHA_VANTAGE_API_KEY
435446
436447 if ( typeof apiKey !== 'string' || apiKey . trim ( ) === '' ) {
437448 await context ?. writer ?. custom ( {
@@ -674,7 +685,7 @@ export const alphaVantageStockTool = createTool({
674685 } )
675686 } ,
676687 onOutput : ( { output, toolCallId, toolName, abortSignal } ) => {
677- const dataKeys = output . data ? Object . keys ( output . data ) . length : 0
688+ const dataKeys = ( output . data !== null && typeof output . data === 'object' ) ? Object . keys ( output . data as object ) . length : 0
678689 log . info ( 'Alpha Vantage stock completed' , {
679690 toolCallId,
680691 toolName,
@@ -804,6 +815,7 @@ export const alphaVantageTool = createTool({
804815 . optional ( ) ,
805816 } ) ,
806817 execute : async ( inputData , context ) => {
818+ const requestContext = context ?. requestContext as AlphaVantageRequestContext
807819 const span = trace
808820 . getTracer ( 'alpha-vantage-tool' , '1.0.0' )
809821 . startSpan ( 'alpha-vantage' , {
@@ -823,7 +835,7 @@ export const alphaVantageTool = createTool({
823835 } ,
824836 id : 'alpha-vantage' ,
825837 } )
826- const apiKey = process . env . ALPHA_VANTAGE_API_KEY
838+ const apiKey = requestContext ?. apiKey ?? process . env . ALPHA_VANTAGE_API_KEY
827839
828840 if ( typeof apiKey !== 'string' || apiKey . trim ( ) === '' ) {
829841 await context ?. writer ?. custom ( {
0 commit comments