createDocToPdfJob
Converts documents to PDF by processing specified object paths in Tencent Cloud Object Storage (COS) using the MCP protocol, enabling file transformation without manual coding.
Instructions
创建文档转 pdf 处理任务
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| objectKey | Yes | 对象在存储桶里的路径 |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"objectKey": {
"description": "对象在存储桶里的路径",
"type": "string"
}
},
"required": [
"objectKey"
],
"type": "object"
}
Implementation Reference
- src/server.ts:532-550 (registration)Registration of the MCP tool 'createDocToPdfJob' with input schema and handler wrapper that calls CIDocService.createDocToPdfJobsserver.tool( 'createDocToPdfJob', '创建文档转 pdf 处理任务', { objectKey: z.string().describe('对象在存储桶里的路径'), }, async ({ objectKey }) => { const res = await CIDocInstance.createDocToPdfJobs(objectKey); return { content: [ { type: 'text', text: JSON.stringify(res.data, null, 2), }, ], isError: !res.isSuccess, }; }, );
- src/services/ci/doc.service.ts:14-142 (handler)Core handler logic for creating document to PDF job: constructs XML request to Tencent COS CI, submits job, polls status up to 10 times using describeDocProcessJobasync createDocToPdfJobs(objectKey: string) { try { var host = this.bucket + '.ci.' + this.region + '.myqcloud.com/doc_jobs'; var url = 'https://' + host; const lastDotIndex = objectKey.lastIndexOf('.'); const base = lastDotIndex === -1 ? objectKey : objectKey.substring(0, lastDotIndex); const now = new Date(); const formattedDate = [ now.getFullYear(), String(now.getMonth() + 1).padStart(2, '0'), // 月份补零 String(now.getDate()).padStart(2, '0'), // 日期补零 ].join(''); const outPutObject = `${formattedDate}_\${SheetID}/${base}_pdf_${generateCode(6)}.pdf`; var body = COS.util.json2xml({ Request: { Tag: 'DocProcess', Input: { Object: objectKey, // 存在cos里的路径 }, Operation: { DocProcess: { TgtType: 'pdf', }, Output: { Bucket: this.bucket, Region: this.region, Object: outPutObject, // 转码后存到cos的路径 }, }, }, }); const createResult = await new Promise((resolve, reject) => { this.cos.request( { Key: 'doc_jobs', Method: 'POST', // 固定值 Url: url, Body: body, ContentType: 'application/xml', }, (error, data) => (error ? reject(error) : resolve(data)), ); }); try { const jobsDetail = (createResult as any).Response.JobsDetail; const initialCode = jobsDetail.Code; const initialState = jobsDetail.State; if (initialCode == 'Failed') { return { isSuccess: false, message: '文档转pdf失败', data: createResult, }; } if (initialState == 'Success') { return { isSuccess: true, message: '文档转pdf成功', data: createResult, }; } else { const jobId = jobsDetail.JobId; // 开始轮询 let pollResult: any; const maxAttempts = 10; const interval = 2000; for (let attempt = 0; attempt < maxAttempts; attempt++) { // 首次立即执行,后续等待间隔 if (attempt > 0) await new Promise((r) => setTimeout(r, interval)); try { // 查询任务状态 const { data: getResult } = await this.describeDocProcessJob(jobId); const describeJobsDetail = (getResult as any).Response.JobsDetail; const describeJobCode = describeJobsDetail.Code; const describeJobState = describeJobsDetail.State; // 处理终态 if ( describeJobCode === 'Success' && describeJobState == 'Success' ) { pollResult = getResult; break; } else if (describeJobCode === 'Failed') { return { isSuccess: false, message: '文档转换失败', data: getResult, }; } } catch (err) { // lastError = err as Error; // 记录错误继续重试 } } if (!pollResult) { return { isSuccess: false, message: `轮询超时(${maxAttempts}次未完成)`, data: createResult, }; } return { isSuccess: true, message: '文档转码成功', data: pollResult, }; } } catch (error) { return { isSuccess: false, message: '文档转pdf失败', data: error, }; } } catch (error) { return { isSuccess: false, message: '文档转pdf失败', data: error, }; } }
- src/server.ts:535-537 (schema)Input schema for the tool using Zod: requires objectKey string (path in bucket){ objectKey: z.string().describe('对象在存储桶里的路径'), },