create_workitem_attachment
Attach a local file to a work item using its absolute path. Supports any file type.
Instructions
[Project Management] Upload a local file as an attachment to a specific work item. The MCP Server reads the file from the given local absolute path and uploads it. Supports any file type.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| organizationId | Yes | Organization ID, can be found in the basic information page of the organization admin console | |
| workItemId | Yes | 工作项唯一标识 | |
| filePath | Yes | 本地文件的绝对路径,MCP Server 将读取该文件并上传为工作项附件 | |
| operatorId | No | 操作者的userId,个人token时该参数无效 |
Implementation Reference
- operations/projex/attachment.ts:111-175 (handler)Core handler function that uploads a local file as an attachment to a work item. Reads the file from disk, constructs a multipart/form-data request using fetch(), and sends it to the Yunxiao API. Handles both region and non-region editions via URL path selection.
export async function createWorkitemAttachmentFunc( organizationId: string | undefined, workItemId: string, filePath: string, operatorId?: string ): Promise<WorkitemFile> { // 验证文件存在 if (!existsSync(filePath)) { throw new Error(`File not found: ${filePath}`); } // 读取文件内容 const fileBuffer = await readFile(filePath); const fileName = path.basename(filePath); const finalOrgId = await resolveOrganizationId(organizationId); const urlPath = isRegionEdition() ? `/oapi/v1/projex/workitems/${workItemId}/attachments` : `/oapi/v1/projex/organizations/${finalOrgId}/workitems/${workItemId}/attachments`; const fullUrl = `${getYunxiaoApiBaseUrl()}${urlPath}`; // 构造 FormData(Node.js 18+ 原生支持) const formData = new FormData(); const blob = new Blob([fileBuffer]); formData.append("file", blob, fileName); if (operatorId) { formData.append("operatorId", operatorId); } // 直接用 fetch 发送 multipart/form-data(不走 yunxiaoRequest,因为它强制 JSON content-type) const requestHeaders: Record<string, string> = { "Accept": "application/json", "User-Agent": `modelcontextprotocol/servers/alibabacloud-devops-mcp-server/v${VERSION} ${getUserAgent()}`, }; const token = getCurrentSessionToken(); if (token) { requestHeaders["x-yunxiao-token"] = token; } const response = await fetch(fullUrl, { method: "POST", headers: requestHeaders, body: formData, } as RequestInit); const contentType = response.headers.get("content-type"); const responseBody = contentType?.includes("application/json") ? await response.json() : await response.text(); if (!response.ok) { throw createYunxiaoError( response.status, responseBody, fullUrl, "POST", requestHeaders, { filePath, fileName } ); } return WorkitemFileSchema.parse(responseBody); } - operations/projex/types.ts:702-707 (schema)Zod schema defining the input parameters: organizationId (required), workItemId (required), filePath (required local absolute path), and operatorId (optional).
export const CreateWorkitemAttachmentSchema = z.object({ organizationId: z.string().describe("Organization ID, can be found in the basic information page of the organization admin console"), workItemId: z.string().describe("工作项唯一标识"), filePath: z.string().describe("本地文件的绝对路径,MCP Server 将读取该文件并上传为工作项附件"), operatorId: z.string().optional().describe("操作者的userId,个人token时该参数无效"), }); - tool-registry/project-management.ts:157-160 (registration)Tool registration in the project management tool registry, defining the tool name, description, and input schema.
name: "create_workitem_attachment", description: "[Project Management] Upload a local file as an attachment to a specific work item. The MCP Server reads the file from the given local absolute path and uploads it. Supports any file type.", inputSchema: zodToJsonSchema(types.CreateWorkitemAttachmentSchema), }, - Tool handler dispatch case that parses arguments using the Zod schema and delegates to createWorkitemAttachmentFunc.
case "create_workitem_attachment": { const args = types.CreateWorkitemAttachmentSchema.parse(request.params.arguments); const result = await attachment.createWorkitemAttachmentFunc( args.organizationId, args.workItemId, args.filePath, args.operatorId ); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], }; } - common/types.ts:116-119 (helper)Re-export of CreateWorkitemAttachmentSchema from operations/projex/types.ts via the common types barrel file.
// Attachment schemas ListWorkitemAttachmentsSchema, GetWorkitemFileSchema, CreateWorkitemAttachmentSchema,