wa_template_get
Retrieve WhatsApp message templates by specifying the template name and language using YCloud WhatsApp API MCP Server.
Instructions
Retrieve a template
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| language | Yes | ||
| name | Yes |
Implementation Reference
- src/tools.ts:160-215 (handler)The generic handler function that executes HTTP requests to the YCloud WhatsApp API for the 'wa_template_get' tool (and other dynamically registered tools). Constructs the URL, sends axios request, and formats response or error.async (args: Record<string, any>) => { try { // 解析URL中的路径参数 let url = `${apiBaseUrl}${path}`; Object.keys(args).forEach(key => { if (path.includes(`{${key}}`)) { url = url.replace(`{${key}}`, encodeURIComponent(String(args[key]))); delete args[key]; } }); // 提取请求体和查询参数 const { body, ...queryParams } = args as Record<string, any>; // 设置请求选项 const requestOptions: any = { url, method: method.toUpperCase(), headers: { 'Content-Type': 'application/json', ...headers }, params: Object.keys(queryParams).length > 0 ? queryParams : undefined, data: body, }; // 发送请求 const response = await axios(requestOptions); return { content: [{ type: 'text' as const, text: JSON.stringify(response.data, null, 2) }] }; } catch (error: unknown) { if (axios.isAxiosError(error) && error.response) { return { content: [{ type: 'text' as const, text: JSON.stringify({ error: true, status: error.response.status, message: error.response.data?.message || error.message, data: error.response.data }, null, 2) }] }; } return { content: [{ type: 'text' as const, text: JSON.stringify({ error: true, message: error instanceof Error ? error.message : String(error) }, null, 2) }] }; } }
- src/tools.ts:91-93 (registration)Logic that maps specific OpenAPI operations (template retrieve by name and language) to the tool name 'wa_template_get' during dynamic registration.operationId.includes('whatsapp_template-retrieve-by-name-and-language')) { operationId = 'wa_template_get'; } else if (operationId.includes('phone_number_update_commerce_settings')) {
- src/tools.ts:8-50 (schema)Helper function to extract Zod schemas for tool parameters from OpenAPI operation definitions, used for 'wa_template_get' input validation.*/ function extractParamsSchema(operation: any): any { const properties: Record<string, any> = {}; const required: string[] = []; // 处理路径参数 if (operation.parameters) { operation.parameters.forEach((param: any) => { if (param.in === 'path' || param.in === 'query') { let schema; switch (param.schema?.type) { case 'string': schema = z.string(); break; case 'integer': schema = z.number().int(); break; case 'number': schema = z.number(); break; case 'boolean': schema = z.boolean(); break; default: schema = z.any(); } properties[param.name] = schema; if (param.required) { required.push(param.name); } } }); } // 处理请求体 if (operation.requestBody) { properties['body'] = z.any(); required.push('body'); } return properties; }
- src/tools.ts:154-219 (registration)The server.tool registration call that registers 'wa_template_get' (and other tools) with name, description, schema, and generic handler.// 检查工具名称是否已注册 if (!registeredTools.has(toolName)) { server.tool( toolName, description, paramsSchema, async (args: Record<string, any>) => { try { // 解析URL中的路径参数 let url = `${apiBaseUrl}${path}`; Object.keys(args).forEach(key => { if (path.includes(`{${key}}`)) { url = url.replace(`{${key}}`, encodeURIComponent(String(args[key]))); delete args[key]; } }); // 提取请求体和查询参数 const { body, ...queryParams } = args as Record<string, any>; // 设置请求选项 const requestOptions: any = { url, method: method.toUpperCase(), headers: { 'Content-Type': 'application/json', ...headers }, params: Object.keys(queryParams).length > 0 ? queryParams : undefined, data: body, }; // 发送请求 const response = await axios(requestOptions); return { content: [{ type: 'text' as const, text: JSON.stringify(response.data, null, 2) }] }; } catch (error: unknown) { if (axios.isAxiosError(error) && error.response) { return { content: [{ type: 'text' as const, text: JSON.stringify({ error: true, status: error.response.status, message: error.response.data?.message || error.message, data: error.response.data }, null, 2) }] }; } return { content: [{ type: 'text' as const, text: JSON.stringify({ error: true, message: error instanceof Error ? error.message : String(error) }, null, 2) }] }; } } ); registeredTools.add(toolName); registeredToolsCount++; }
- src/index.ts:42-43 (registration)Calls the registerYCloudTools function to dynamically register all tools including 'wa_template_get' on the MCP server.const openApiSpec = await loadOpenApiSpec(openApiSpecPath); await registerYCloudTools(server, openApiSpec, apiBaseUrl, headers);