@iqai/adk 0.0.9 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -2186,6 +2186,7 @@ __export(tools_exports, {
2186
2186
  LoadMemoryTool: () => LoadMemoryTool,
2187
2187
  McpError: () => McpError,
2188
2188
  McpErrorType: () => McpErrorType,
2189
+ McpSamplingHandler: () => McpSamplingHandler,
2189
2190
  McpToolset: () => McpToolset,
2190
2191
  ToolContext: () => ToolContext,
2191
2192
  TransferToAgentTool: () => TransferToAgentTool,
@@ -2193,6 +2194,7 @@ __export(tools_exports, {
2193
2194
  adkToMcpToolType: () => adkToMcpToolType,
2194
2195
  buildFunctionDeclaration: () => buildFunctionDeclaration,
2195
2196
  createFunctionTool: () => createFunctionTool,
2197
+ createSamplingHandler: () => createSamplingHandler,
2196
2198
  getMcpTools: () => getMcpTools,
2197
2199
  jsonSchemaToDeclaration: () => jsonSchemaToDeclaration,
2198
2200
  mcpSchemaToParameters: () => mcpSchemaToParameters,
@@ -2979,15 +2981,24 @@ var McpError = class extends Error {
2979
2981
  // src/tools/mcp/sampling-handler.ts
2980
2982
  var McpSamplingHandler = class {
2981
2983
  logger = new Logger({ name: "McpSamplingHandler" });
2982
- adkHandler;
2983
- constructor(adkHandler) {
2984
- this.adkHandler = adkHandler;
2984
+ samplingHandler;
2985
+ constructor(samplingHandler) {
2986
+ this.samplingHandler = samplingHandler;
2985
2987
  }
2986
2988
  /**
2987
2989
  * Handle MCP sampling request and convert between formats
2988
2990
  */
2989
2991
  async handleSamplingRequest(request) {
2990
2992
  try {
2993
+ if (request.method !== "sampling/createMessage") {
2994
+ this.logger.error(
2995
+ `Invalid method for sampling handler: ${request.method}. Expected: sampling/createMessage`
2996
+ );
2997
+ throw new McpError(
2998
+ `Invalid method: ${request.method}. This handler only processes sampling/createMessage requests.`,
2999
+ "INVALID_REQUEST_ERROR" /* INVALID_REQUEST_ERROR */
3000
+ );
3001
+ }
2991
3002
  const validationResult = CreateMessageRequestSchema.safeParse(request);
2992
3003
  if (!validationResult.success) {
2993
3004
  this.logger.error(
@@ -3013,19 +3024,19 @@ var McpSamplingHandler = class {
3013
3024
  );
3014
3025
  }
3015
3026
  this.logger.debug("Converting MCP request to ADK format");
3016
- const adkMessages = this.convertMcpMessagesToADK(mcpParams.messages);
3027
+ const adkMessages = this.convertMcpMessagesToADK(
3028
+ mcpParams.messages,
3029
+ mcpParams.systemPrompt
3030
+ );
3017
3031
  const adkRequest = {
3018
3032
  messages: adkMessages,
3019
- systemPrompt: mcpParams.systemPrompt,
3020
- modelPreferences: mcpParams.modelPreferences,
3021
- includeContext: mcpParams.includeContext,
3022
- temperature: mcpParams.temperature,
3023
- maxTokens: mcpParams.maxTokens,
3024
- stopSequences: mcpParams.stopSequences,
3025
- metadata: mcpParams.metadata
3033
+ config: {
3034
+ temperature: mcpParams.temperature,
3035
+ max_tokens: mcpParams.maxTokens
3036
+ }
3026
3037
  };
3027
3038
  this.logger.debug("Calling ADK sampling handler");
3028
- const adkResponse = await this.adkHandler(adkRequest);
3039
+ const adkResponse = await this.samplingHandler(adkRequest);
3029
3040
  this.logger.debug("Converting ADK response to MCP format");
3030
3041
  const mcpResponse = this.convertADKResponseToMcp(adkResponse);
3031
3042
  const responseValidation = CreateMessageResultSchema.safeParse(mcpResponse);
@@ -3055,54 +3066,66 @@ var McpSamplingHandler = class {
3055
3066
  /**
3056
3067
  * Convert MCP messages to ADK message format
3057
3068
  */
3058
- convertMcpMessagesToADK(mcpMessages) {
3059
- return mcpMessages.map((mcpMessage) => {
3060
- const adkRole = mcpMessage.role === "assistant" ? "assistant" : "user";
3061
- let adkContent;
3062
- if (mcpMessage.content.type === "text") {
3063
- adkContent = mcpMessage.content.text || "";
3064
- } else if (mcpMessage.content.type === "image") {
3065
- const contentParts = [];
3066
- if (mcpMessage.content.text) {
3067
- contentParts.push({
3068
- type: "text",
3069
- text: mcpMessage.content.text
3070
- });
3071
- }
3072
- if (mcpMessage.content.data) {
3073
- const mimeType = mcpMessage.content.mimeType || "image/jpeg";
3074
- const dataUrl = `data:${mimeType};base64,${mcpMessage.content.data}`;
3075
- contentParts.push({
3076
- type: "image",
3077
- image_url: {
3078
- url: dataUrl
3079
- }
3080
- });
3081
- }
3082
- adkContent = contentParts.length > 0 ? contentParts : "";
3083
- } else {
3084
- this.logger.warn(
3085
- `Unknown MCP content type: ${mcpMessage.content.type}`
3086
- );
3087
- adkContent = mcpMessage.content.data || "";
3088
- }
3089
- const adkMessage = {
3090
- role: adkRole,
3091
- content: adkContent
3092
- };
3093
- this.logger.debug(
3094
- `Converted MCP message - role: ${mcpMessage.role} -> ${adkRole}, content type: ${mcpMessage.content.type}`
3095
- );
3096
- return adkMessage;
3069
+ convertMcpMessagesToADK(mcpMessages, systemPrompt) {
3070
+ const transformedMessages = mcpMessages.map(
3071
+ (mcpMessage) => this.convertSingleMcpMessageToADK(mcpMessage)
3072
+ );
3073
+ transformedMessages.unshift({
3074
+ role: "system",
3075
+ content: systemPrompt
3097
3076
  });
3077
+ return transformedMessages;
3078
+ }
3079
+ /**
3080
+ * Convert a single MCP message to ADK message format
3081
+ */
3082
+ convertSingleMcpMessageToADK(mcpMessage) {
3083
+ const adkRole = mcpMessage.role === "assistant" ? "assistant" : "user";
3084
+ const adkContent = this.convertMcpContentToADK(mcpMessage.content);
3085
+ const adkMessage = {
3086
+ role: adkRole,
3087
+ content: adkContent
3088
+ };
3089
+ this.logger.debug(
3090
+ `Converted MCP message - role: ${mcpMessage.role} -> ${adkRole}, content type: ${mcpMessage.content.type}`
3091
+ );
3092
+ return adkMessage;
3093
+ }
3094
+ /**
3095
+ * Convert MCP message content to ADK content format
3096
+ */
3097
+ convertMcpContentToADK(mcpContent) {
3098
+ if (mcpContent.type === "text") {
3099
+ return mcpContent.text || "";
3100
+ }
3101
+ if (mcpContent.type === "image") {
3102
+ const contentParts = [];
3103
+ if (mcpContent.text) {
3104
+ contentParts.push({
3105
+ type: "text",
3106
+ text: mcpContent.text
3107
+ });
3108
+ }
3109
+ if (mcpContent.data) {
3110
+ const mimeType = mcpContent.mimeType || "image/jpeg";
3111
+ const dataUrl = `data:${mimeType};base64,${mcpContent.data}`;
3112
+ contentParts.push({
3113
+ type: "image",
3114
+ image_url: {
3115
+ url: dataUrl
3116
+ }
3117
+ });
3118
+ }
3119
+ return contentParts.length > 0 ? contentParts : "";
3120
+ }
3121
+ this.logger.warn(`Unknown MCP content type: ${mcpContent.type}`);
3122
+ return mcpContent.data || "";
3098
3123
  }
3099
3124
  /**
3100
3125
  * Convert ADK response to MCP response format
3101
3126
  */
3102
3127
  convertADKResponseToMcp(adkResponse) {
3103
3128
  const mcpResponse = {
3104
- model: adkResponse.model,
3105
- stopReason: adkResponse.stopReason,
3106
3129
  role: "assistant",
3107
3130
  // ADK responses are always from assistant
3108
3131
  content: {
@@ -3110,19 +3133,20 @@ var McpSamplingHandler = class {
3110
3133
  text: adkResponse.content || ""
3111
3134
  }
3112
3135
  };
3113
- this.logger.debug(
3114
- `Converted ADK response - model: ${adkResponse.model}, content length: ${adkResponse.content?.length || 0}`
3115
- );
3136
+ this.logger.debug(`Received content: ${adkResponse.content}`);
3116
3137
  return mcpResponse;
3117
3138
  }
3118
3139
  /**
3119
3140
  * Update the ADK handler
3120
3141
  */
3121
3142
  updateHandler(handler) {
3122
- this.adkHandler = handler;
3143
+ this.samplingHandler = handler;
3123
3144
  this.logger.debug("ADK sampling handler updated");
3124
3145
  }
3125
3146
  };
3147
+ function createSamplingHandler(handler) {
3148
+ return handler;
3149
+ }
3126
3150
 
3127
3151
  // src/tools/mcp/utils.ts
3128
3152
  function withRetry(fn, instance, reinitMethod, maxRetries = 1) {
@@ -3162,6 +3186,9 @@ var McpClientService = class {
3162
3186
  logger = new Logger({ name: "McpClientService" });
3163
3187
  constructor(config) {
3164
3188
  this.config = config;
3189
+ if (config.samplingHandler) {
3190
+ this.mcpSamplingHandler = new McpSamplingHandler(config.samplingHandler);
3191
+ }
3165
3192
  }
3166
3193
  /**
3167
3194
  * Initializes and returns an MCP client based on configuration.
@@ -3190,7 +3217,9 @@ var McpClientService = class {
3190
3217
  capabilities: {
3191
3218
  prompts: {},
3192
3219
  resources: {},
3193
- tools: {}
3220
+ tools: {},
3221
+ sampling: {}
3222
+ // Enable sampling capability
3194
3223
  }
3195
3224
  }
3196
3225
  );
@@ -3396,13 +3425,13 @@ var McpClientService = class {
3396
3425
  }
3397
3426
  }
3398
3427
  /**
3399
- * Set an ADK sampling handler
3428
+ * Set a new ADK sampling handler
3400
3429
  */
3401
3430
  setSamplingHandler(handler) {
3402
3431
  this.mcpSamplingHandler = new McpSamplingHandler(handler);
3403
3432
  if (this.client) {
3404
3433
  this.setupSamplingHandler(this.client).catch((error) => {
3405
- console.error("Failed to update sampling handler:", error);
3434
+ console.error("Failed to update ADK sampling handler:", error);
3406
3435
  });
3407
3436
  }
3408
3437
  }
@@ -3753,6 +3782,32 @@ var McpToolset = class {
3753
3782
  await this.clientService.initialize();
3754
3783
  return this.clientService;
3755
3784
  }
3785
+ /**
3786
+ * Set a sampling handler for this MCP toolset.
3787
+ * This allows MCP servers to request LLM completions through your ADK agent.
3788
+ *
3789
+ * @param handler - ADK sampling handler that receives ADK-formatted messages
3790
+ */
3791
+ setSamplingHandler(handler) {
3792
+ if (!this.clientService) {
3793
+ this.clientService = new McpClientService(this.config);
3794
+ }
3795
+ this.clientService.setSamplingHandler(handler);
3796
+ if (this.config.debug) {
3797
+ console.log("\u{1F3AF} Sampling handler set for MCP toolset");
3798
+ }
3799
+ }
3800
+ /**
3801
+ * Remove the sampling handler
3802
+ */
3803
+ removeSamplingHandler() {
3804
+ if (this.clientService) {
3805
+ this.clientService.removeSamplingHandler();
3806
+ if (this.config.debug) {
3807
+ console.log("\u{1F6AB} Sampling handler removed from MCP toolset");
3808
+ }
3809
+ }
3810
+ }
3756
3811
  /**
3757
3812
  * Retrieves tools from the MCP server and converts them to BaseTool instances.
3758
3813
  * Similar to Python's get_tools method.
@@ -7167,6 +7222,7 @@ export {
7167
7222
  LoopAgent,
7168
7223
  McpError,
7169
7224
  McpErrorType,
7225
+ McpSamplingHandler,
7170
7226
  McpToolset,
7171
7227
  memory_exports as Memory,
7172
7228
  models_exports as Models,
@@ -7195,6 +7251,7 @@ export {
7195
7251
  buildFunctionDeclaration,
7196
7252
  cloneSession,
7197
7253
  createFunctionTool,
7254
+ createSamplingHandler,
7198
7255
  generateSessionId,
7199
7256
  getMcpTools,
7200
7257
  jsonSchemaToDeclaration,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@iqai/adk",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "description": "Agent Development Kit for TypeScript with multi-provider LLM support",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",