adk-typescript
Version:
TypeScript port of Google's Agent Development Kit (ADK)
146 lines (145 loc) • 5.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaType = void 0;
exports.adkToMcpToolType = adkToMcpToolType;
exports.geminiToJsonSchema = geminiToJsonSchema;
/**
* Convert a Tool in ADK into MCP tool type.
*
* This function transforms an ADK tool definition into its equivalent
* representation in the MCP (Model Control Plane) system.
*
* @param tool The ADK tool to convert
* @returns An object of MCP Tool type
*/
function adkToMcpToolType(tool) {
// Get the function declaration from the tool
const functionDeclaration = tool.getDeclaration();
// If no declaration is available, use empty schema
const inputSchema = functionDeclaration ?
geminiToJsonSchema(functionDeclaration.parameters) :
{ type: "object", properties: {} };
return {
name: tool.name,
description: tool.description,
inputSchema: inputSchema
};
}
/**
* Schema type enum mapping between Gemini and JSON Schema
*/
var SchemaType;
(function (SchemaType) {
SchemaType["TYPE_UNSPECIFIED"] = "null";
SchemaType["STRING"] = "string";
SchemaType["NUMBER"] = "number";
SchemaType["INTEGER"] = "integer";
SchemaType["BOOLEAN"] = "boolean";
SchemaType["ARRAY"] = "array";
SchemaType["OBJECT"] = "object";
})(SchemaType || (exports.SchemaType = SchemaType = {}));
/**
* Convert a Gemini schema to JSON schema
* @param geminiSchema The Gemini schema to convert
* @returns The equivalent JSON schema
*/
function geminiToJsonSchema(geminiSchema) {
if (!geminiSchema || typeof geminiSchema !== 'object') {
throw new TypeError(`Input must be an object, got ${typeof geminiSchema}`);
}
const jsonSchema = {};
// Map Type
if (geminiSchema.type) {
jsonSchema.type = geminiSchema.type.toLowerCase();
}
else {
jsonSchema.type = 'object';
}
// Map Nullable
if (geminiSchema.nullable === true) {
// In JSON Schema, nullable is handled with type: ['type', 'null']
jsonSchema.type = [jsonSchema.type, 'null'];
}
// Map direct fields
const directMappings = {
title: 'title',
description: 'description',
default: 'default',
enum: 'enum',
format: 'format',
example: 'example',
};
for (const [geminiKey, jsonKey] of Object.entries(directMappings)) {
if (geminiSchema[geminiKey] !== undefined) {
jsonSchema[jsonKey] = geminiSchema[geminiKey];
}
}
// String validation
if (geminiSchema.type === SchemaType.STRING) {
const strMappings = {
pattern: 'pattern',
minLength: 'minLength',
maxLength: 'maxLength',
};
for (const [geminiKey, jsonKey] of Object.entries(strMappings)) {
if (geminiSchema[geminiKey] !== undefined) {
jsonSchema[jsonKey] = geminiSchema[geminiKey];
}
}
}
// Number/Integer validation
if (geminiSchema.type === SchemaType.NUMBER || geminiSchema.type === SchemaType.INTEGER) {
const numMappings = {
minimum: 'minimum',
maximum: 'maximum',
};
for (const [geminiKey, jsonKey] of Object.entries(numMappings)) {
if (geminiSchema[geminiKey] !== undefined) {
jsonSchema[jsonKey] = geminiSchema[geminiKey];
}
}
}
// Array validation (recursive call for items)
if (geminiSchema.type === SchemaType.ARRAY) {
if (geminiSchema.items) {
jsonSchema.items = geminiToJsonSchema(geminiSchema.items);
}
const arrMappings = {
minItems: 'minItems',
maxItems: 'maxItems',
};
for (const [geminiKey, jsonKey] of Object.entries(arrMappings)) {
if (geminiSchema[geminiKey] !== undefined) {
jsonSchema[jsonKey] = geminiSchema[geminiKey];
}
}
}
// Object validation (recursive call for properties)
if (geminiSchema.type === SchemaType.OBJECT) {
if (geminiSchema.properties) {
jsonSchema.properties = {};
for (const [propName, propSchema] of Object.entries(geminiSchema.properties)) {
jsonSchema.properties[propName] = geminiToJsonSchema(propSchema);
}
}
else {
// Ensure object schema has properties
jsonSchema.properties = {};
}
const objMappings = {
required: 'required',
minProperties: 'minProperties',
maxProperties: 'maxProperties',
};
for (const [geminiKey, jsonKey] of Object.entries(objMappings)) {
if (geminiSchema[geminiKey] !== undefined) {
jsonSchema[jsonKey] = geminiSchema[geminiKey];
}
}
}
// Map anyOf (recursive call for subschemas)
if (geminiSchema.anyOf) {
jsonSchema.anyOf = geminiSchema.anyOf.map(subSchema => geminiToJsonSchema(subSchema));
}
return jsonSchema;
}