mcp abap adt笔记二(debug)
2026/8/22 12:46:46 网站建设 项目流程

GitHub在线库:https://github.com/mario-andreschak/mcp-abap-abap-adt-api

Tips:可以在github后面加1s,切换到VS Code阅读源码的模式。例如这样

GitHub1s

目标:

本篇blog通过使用mcp-abap-adt-api的debug相关工具,学习了解MCP Tool

1.MCP Tool定义

index.ts ->setupToolHandlers()

然后调用getTools方法

import { DebugHandlers } from './handlers/DebugHandlers.js'; ...this.debugHandlers.getTools()

然后就可以看到src/handlers/DebugHandlers.ts例如一个debuggerSetBreakpoints设置断点的MCP Tool 定义,核心是下面这三个要素:

  1. name(名称):工具的唯一标识符,AI 将通过这个名字来调用它。名称需要清晰且符合规范(如使用字母、数字、下划线)。

  2. description(描述):一段人类(和 AI)可读的文本,用来说明工具的功能和适用场景。一个准确的描述能帮助 AI 在合适的时机选择正确的工具。

  3. inputSchema(输入参数结构):这是最核心的部分,它使用 JSON Schema 格式来定义调用该工具时,需要传入哪些参数,每个参数的类型、是否必填等。JSON Schema 是一种通用的、标准化的参数描述方式。

  4. 定义代码

    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): Promise<any> { switch (toolName) { case 'debuggerSetBreakpoints': return this.handleDebuggerSetBreakpoints(args); } } async handleDebuggerSetBreakpoints(args: any): Promise<any> { 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/breakpoints

HTTP方法:POST

Headers:

KeyValue

Authorization

Basic ...

X-CSRF-Token

先 Fetch 拿到的值

X-sap-adt-sessiontype

stateful

Content-Type

application/xml

Accept

application/xml

Body(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)[]>;

参数说明

参数类型必填默认说明

1

debuggingMode

"user" | "terminal"

"user":按 SAP 用户断;"terminal":按终端会话断

2

terminalId

string

调试终端 ID(自拟 GUID/字符串)。与后续 debuggerListen / debuggerDeleteListener 保持一致

3

ideId

string

IDE 会话 ID(自拟)。与 Listen/Delete 保持一致

4

clientId

string

本批断点的客户端 ID;写入每个 <breakpoint clientId="...">

5

breakpoints

(string | DebugBreakpoint)[]

断点列表。字符串 = 行断点 URI;或传完整 DebugBreakpoint 对象

6

user

string

"user" 模式必填

要拦截的 SAP 用户(建议大写)。对应 XML requestUser

7

scope

"external" | "debugger"

"external"

断点作用域。外部会话调试用 "external"

8

systemDebugging

boolean

false

是否系统调试

9

deactivated

boolean

false

true 时下发但不激活

10

syncScupeUrl

string

""

非空则 <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

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询