mcp abap adt笔记二(debug)

📅 2026/8/22 12:46:51
mcp abap adt笔记二(debug)
GitHub在线库:https://github.com/mario-andreschak/mcp-abap-abap-adt-apiTips:可以在github后面加1s,切换到VS Code阅读源码的模式。例如这样GitHub1s目标:本篇blog通过使用mcp-abap-adt-api的debug相关工具,学习了解MCPTool1.MCP Tool定义index.ts -setupToolHandlers()然后调用getTools方法import { DebugHandlers } from './handlers/DebugHandlers.js'; ...this.debugHandlers.getTools()然后就可以看到src/handlers/DebugHandlers.ts例如一个debuggerSetBreakpoints设置断点的MCP Tool 定义,核心是下面这三个要素:name(名称):工具的唯一标识符,AI 将通过这个名字来调用它。名称需要清晰且符合规范(如使用字母、数字、下划线)。description(描述):一段人类(和 AI)可读的文本,用来说明工具的功能和适用场景。一个准确的描述能帮助 AI 在合适的时机选择正确的工具。inputSchema(输入参数结构):这是最核心的部分,它使用JSON Schema格式来定义调用该工具时,需要传入哪些参数,每个参数的类型、是否必填等。JSON Schema 是一种通用的、标准化的参数描述方式。定义代码getTools(): ToolDefinition[] { return [ { name: 'debuggerSetBreakpoints', description: 'Sets breakpoints.', inputSchema: { type: 'object', properties: { debuggingMode: { type: 'string', description: 'The debugging mode.' }, terminalId: { type: 'string', description: 'The terminal ID.' }, ideId: { type: 'string', description: 'The IDE ID.' }, clientId: { type: 'string', description: 'The client ID.' }, breakpoints: { type: 'array', description: 'An array of breakpoints.' }, user: { type: 'string', description: 'The user.' }, scope: { type: 'string', description: 'The debugger scope.', optional: true }, systemDebugging: { type: 'boolean', description: 'Whether to enable system debugging.', optional: true }, deactivated: { type: 'boolean', description: 'Whether to deactivate the breakpoints.', optional: true }, syncScupeUrl: { type: 'string', description: 'The URL for scope synchronization.', optional: true } }, required: ['debuggingMode', 'terminalId', 'ideId', 'clientId', 'breakpoints', 'user'] } } ]; }2.MCP Tool实现async handle(toolName: string, args: any): Promiseany { switch (toolName) { case 'debuggerSetBreakpoints': return this.handleDebuggerSetBreakpoints(args); } } async handleDebuggerSetBreakpoints(args: any): Promiseany { const startTime = performance.now(); try { const result = await this.adtclient.debuggerSetBreakpoints( args.debuggingMode, args.terminalId, args.ideId, args.clientId, args.breakpoints, args.user, args.scope, args.systemDebugging, args.deactivated, args.syncScupeUrl ); this.trackRequest(startTime, true); return { content: [ { type: 'text', text: JSON.stringify({ status: 'success', result }) } ] }; } catch (error: any) { this.trackRequest(startTime, false); throw new McpError( ErrorCode.InternalError, `Failed to set breakpoints: ${error.message || 'Unknown error'}` ); } }3.本质上是调用的adt封装好的rest api实现的打断点服务URL:http://ip:port/sap/bc/adt/debugger/breakpointsHTTP方法:POSTHeaders:KeyValueAuthorizationBasic ...X-CSRF-Token先 Fetch 拿到的值X-sap-adt-sessiontypestatefulContent-Typeapplication/xmlAcceptapplication/xmlBody(raw XML)示例——给类ycl_demo01第 29行打断点:?xml version="1.0" encoding="UTF-8"? dbg:breakpoints xmlns:dbg="http://www.sap.com/adt/debugger" scope="external" debuggingMode="user" requestUser="BAKKF" terminalId="POSTMAN-TERM-1" ideId="POSTMAN-IDE-1" systemDebugging="false" deactivated="false" syncScope mode="full"/syncScope breakpoint xmlns:adtcore="http://www.sap.com/adt/core" kind="line" clientId="bp-postman-1" skipCount="0" adtcore:uri="/sap/bc/adt/oo/classes/ycl_demo01/source/main#start=29"/ /dbg:breakpoints成功返回报文?xml version="1.0" encoding="utf-8"? dbg:breakpoints xmlns:dbg="http://www.sap.com/adt/debugger" breakpoint kind="line" clientId="bp-postman-1" nonAbapFlavour="" adtcore:uri="/sap/bc/adt/oo/classes/ycl_demo01/source/main#start=29" adtcore:type="CLAS/OC" adtcore:name="YCL_DEMO01" xmlns:adtcore="http://www.sap.com/adt/core"/ /dbg:breakpoints当然ADTClient有了很好的封装,如果使用ADTClient的debuggerSetBreakpoints方法,不需要自己拼接xml报文,简单非常多。只需要传这些参数,例如a.debuggerSetBreakpoints( 'user', 'TERM-01', 'IDE-01', 'BP-01', ['/sap/bc/adt/oo/classes/ycl_demo01/source/main#start=29'], 'BAKKF', 'external' );debuggerSetBreakpoints方法的参数定义如下:debuggerSetBreakpoints(debuggingMode: "user", terminalId: string, ideId: string, clientId: string, breakpoints: (string | DebugBreakpoint)[], user: string, scope?: DebuggerScope, systemDebugging?: boolean, deactivated?: boolean, syncScupeUrl?: string): Promise(DebugBreakpoint | DebugBreakpointError)[];参数说明参数类型必填默认说明1debuggingMode"user" | "terminal"是—"user":按 SAP 用户断;"terminal":按终端会话断2terminalIdstring是—调试终端 ID(自拟 GUID/字符串)。与后续debuggerListen/debuggerDeleteListener保持一致3ideIdstring是—IDE 会话 ID(自拟)。与 Listen/Delete保持一致4clientIdstring是—本批断点的客户端 ID;写入每个breakpoint clientId="..."5breakpoints(string | DebugBreakpoint)[]是—断点列表。字符串= 行断点 URI;或传完整DebugBreakpoint对象6userstring"user"模式必填—要拦截的 SAP 用户(建议大写)。对应 XMLrequestUser7scope"external" | "debugger"否"external"断点作用域。外部会话调试用"external"8systemDebuggingboolean否false是否系统调试9deactivatedboolean否falsetrue时下发但不激活10syncScupeUrlstring否""非空则syncScope mode="partial"并带对象引用;空则mode="full"4.实际用AI调用MCP Tool打断点4.1 调用链路4.2 辅助测试的ABAP类实现接口if_oo_adt_classrun,后续MCP 的runClass工具可以直接运行,方便测试CLASS ycl_demo01 DEFINITION PUBLIC FINAL CREATE PUBLIC. PUBLIC SECTION. INTERFACES if_oo_adt_classrun. METHODS run IMPORTING out TYPE REF TO if_oo_adt_classrun_out. PROTECTED SECTION. PRIVATE SECTION. ENDCLASS. CLASS ycl_d