schedule_background_job
Schedule an ABAP background job to run in SAP by specifying the program, job name, and scramble option.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| program_name | Yes | Name of the ABAP program to execute | |
| scrambleoption | Yes | Scramble option to send to SAP, for example SCRD | |
| job_name | Yes | Name to assign to the background job |
Implementation Reference
- src/index.ts:35-92 (handler)The async handler function that executes the tool logic. Generates a job ID/timestamp, calls scrambleAll() with the scrambleoption, and returns a success/failure response with request details and SAP result.
async ({ program_name, scrambleoption, job_name }) => { const jobId = `JOB_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`; const scheduledAt = new Date().toISOString(); try { const result = await scrambleAll(scrambleoption); return { content: [ { type: "text", text: JSON.stringify( { success: true, message: "ScrambleAll submitted successfully", request: { program_name, scrambleoption, job_name, job_id: jobId, scheduled_at: scheduledAt }, sap_result: result }, null, 2 ) } ] }; } catch (error: any) { return { content: [ { type: "text", text: JSON.stringify( { success: false, message: "SAP call failed", request: { program_name, scrambleoption, job_name, job_id: jobId, scheduled_at: scheduledAt }, error: error?.message || "Unknown error" }, null, 2 ) } ], isError: true }; } } ); - src/index.ts:30-34 (schema)Input schema for the tool using Zod: program_name (string), scrambleoption (string), job_name (string).
{ program_name: z.string().describe("Name of the ABAP program to execute"), scrambleoption: z.string().describe("Scramble option to send to SAP, for example SCRD"), job_name: z.string().describe("Name to assign to the background job") }, - src/index.ts:28-92 (registration)Tool registration via server.tool() with the name 'schedule_background_job'.
server.tool( "schedule_background_job", { program_name: z.string().describe("Name of the ABAP program to execute"), scrambleoption: z.string().describe("Scramble option to send to SAP, for example SCRD"), job_name: z.string().describe("Name to assign to the background job") }, async ({ program_name, scrambleoption, job_name }) => { const jobId = `JOB_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`; const scheduledAt = new Date().toISOString(); try { const result = await scrambleAll(scrambleoption); return { content: [ { type: "text", text: JSON.stringify( { success: true, message: "ScrambleAll submitted successfully", request: { program_name, scrambleoption, job_name, job_id: jobId, scheduled_at: scheduledAt }, sap_result: result }, null, 2 ) } ] }; } catch (error: any) { return { content: [ { type: "text", text: JSON.stringify( { success: false, message: "SAP call failed", request: { program_name, scrambleoption, job_name, job_id: jobId, scheduled_at: scheduledAt }, error: error?.message || "Unknown error" }, null, 2 ) } ], isError: true }; } } ); - src/sap.ts:42-81 (helper)The scrambleAll helper function called by the handler. Fetches a CSRF token from SAP, then POSTs the scrambleoption to the SAP action endpoint and returns the API response with status/headers/data.
export async function scrambleAll(scrambleoption: string) { const { token, cookies } = await getCsrfToken(); const res = await axios.post( `${baseUrl}/${actionPath}`, { scrambleoption }, { auth: { username, password }, params: { "sap-client": client, "sap-language": "EN" }, headers: { "X-CSRF-Token": token, Cookie: cookies, "Content-Type": "application/json", Accept: "application/json" }, httpsAgent: agent, validateStatus: () => true } ); if (res.status < 200 || res.status >= 300) { throw new Error( `SAP call failed: ${res.status} ${JSON.stringify(res.data)}` ); } return { success: true, status: res.status, headers: { sap_messages: res.headers["sap-messages"], location: res.headers["location"], preference_applied: res.headers["preference-applied"] }, data: res.data }; }