mcp-server-bugzilla4 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -35,7 +35,7 @@ BUGZILLA_PASSWORD=xxx npx -y mcp-server-bugzilla4@latest verify --url https://bu
35
35
  "BUGZILLA_URL": "https://bugzilla.example.com",
36
36
  "BUGZILLA_LOGIN": "me@example.com",
37
37
  "BUGZILLA_PASSWORD": "your-password",
38
- "BUGZILLA_PROTOCOL": "jsonrpc",
38
+ "BUGZILLA_PROTOCOL": "auto",
39
39
  "BUGZILLA_PRIORITY_WEIGHTS": "P0=5,P1=3,P2=1,P3=0.2"
40
40
  }
41
41
  }
@@ -49,9 +49,12 @@ BUGZILLA_PASSWORD=xxx npx -y mcp-server-bugzilla4@latest verify --url https://bu
49
49
 
50
50
  | 协议 | 接口 | 适用情况 |
51
51
  | :--- | :--- | :--- |
52
- | `jsonrpc`(默认) | `POST /jsonrpc.cgi` | 服务器装了 Perl 模块 `JSON::RPC`。如果返回 HTML 页面,工具会提示改用 xmlrpc |
52
+ | `auto`(默认) | 先 `jsonrpc.cgi`,必要时 `xmlrpc.cgi` | JSON-RPC 返回 Perl 运行时错误(如 `Not a HASH reference at Bugzilla/WebService/User.pm line 53`)或 HTML 页面时,自动切换到 XML-RPC 并重新登录,之后一直使用 XML-RPC |
53
+ | `jsonrpc` | `POST /jsonrpc.cgi` | 服务器装了与 4.0 兼容的 Perl 模块 `JSON::RPC` |
53
54
  | `xmlrpc` | `POST /xmlrpc.cgi` | 4.0 默认可用,兼容性最好 |
54
55
 
56
+ `Not a HASH reference at Bugzilla/WebService/User.pm line 53` 出现在 `User.login` 读取参数时,通常是服务器上的 `JSON::RPC` 模块升级到了 1.x,与 Bugzilla 4.0 的 JSON-RPC 实现不兼容(4.0 依赖 0.9x 的旧接口)。XML-RPC 不经过这个模块,因此 `auto` 模式能绕过。`verify_auth` 的 `protocol_in_use` 和 `protocol_fallback_reason` 会显示实际使用的协议和切换原因。如果两种协议都失败,说明 WebService 在服务端不可用,需要管理员检查 `JSON::RPC`、`SOAP::Lite` 等依赖。
57
+
55
58
  在浏览器里打开 `https://你的bugzilla/jsonrpc.cgi`,看到 JSON 格式的报错说明 JSON-RPC 可用;看到 500 或依赖缺失的报错就用 xmlrpc。
56
59
 
57
60
  ### 认证方式
@@ -113,7 +116,7 @@ Bugzilla 自带的优先级取值通常不是 `P0`~`P3`(常见为 `P1`~`P5`
113
116
  | 环境变量 | 命令行参数 | 说明 |
114
117
  | :--- | :--- | :--- |
115
118
  | `BUGZILLA_URL` | `--url` | 必填,Bugzilla 根地址(不带 `xmlrpc.cgi`),默认只允许 HTTPS |
116
- | `BUGZILLA_PROTOCOL` | `--protocol` | `jsonrpc`(默认)/ `xmlrpc` |
119
+ | `BUGZILLA_PROTOCOL` | `--protocol` | `auto`(默认)/ `jsonrpc` / `xmlrpc` |
117
120
  | `BUGZILLA_RPC_PATH` | `--rpc-path` | 接口路径,默认 `jsonrpc.cgi` / `xmlrpc.cgi` |
118
121
  | `BUGZILLA_AUTH_TYPE` | `--auth-type` | `cookie`(默认)/ `params` / `none` |
119
122
  | `BUGZILLA_LOGIN` | `--login` | 登录账号(通常是邮箱) |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-server-bugzilla4",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "面向 Bugzilla 4.x 的 MCP Server(JSON-RPC / XML-RPC):Bug 状态 / 版本 Bug 列表 / 版本 Bug 权重 / 历史 / 评论 / 更新 / 创建。npx 免安装使用",
5
5
  "keywords": [
6
6
  "mcp",
package/src/index.js CHANGED
@@ -13,7 +13,7 @@ const HELP = `${PACKAGE.name} v${PACKAGE.version} —— 面向 Bugzilla 4.x 的
13
13
 
14
14
  连接选项(也可用括号内的环境变量配置,命令行优先):
15
15
  --url <url> Bugzilla 根地址,必须 HTTPS(BUGZILLA_URL)
16
- --protocol <p> jsonrpc | xmlrpc,默认 jsonrpc(BUGZILLA_PROTOCOL)
16
+ --protocol <p> auto | jsonrpc | xmlrpc,默认 auto(JSON-RPC 出错时自动切到 XML-RPC)(BUGZILLA_PROTOCOL)
17
17
  --rpc-path <path> 接口路径,默认 jsonrpc.cgi / xmlrpc.cgi(BUGZILLA_RPC_PATH)
18
18
  --auth-type <type> cookie | params | none,默认有账号时 cookie(BUGZILLA_AUTH_TYPE)
19
19
  --login <email> 登录账号(BUGZILLA_LOGIN)
@@ -80,6 +80,7 @@ async function main() {
80
80
  const config = loadConfig(env);
81
81
  process.stderr.write(`配置:${JSON.stringify(describeConfig(config))}\n`);
82
82
  const client = new BugzillaRpcClient(config);
83
+ client.onProtocolSwitch = (e) => process.stderr.write(`JSON-RPC 服务端出错,已自动切换到 XML-RPC:${e.message}\n`);
83
84
  const version = await client.version().then((r) => r?.version);
84
85
  process.stderr.write(`Bugzilla 版本:${version}\n`);
85
86
  if (config.authType === 'none') {
package/src/lib/config.js CHANGED
@@ -36,9 +36,11 @@ export function loadConfig(env = process.env) {
36
36
  throw new Error('BUGZILLA_URL 填 Bugzilla 根地址即可(如 https://bugzilla.example.com),不要带 jsonrpc.cgi / xmlrpc.cgi');
37
37
  }
38
38
 
39
- const protocol = (env.BUGZILLA_PROTOCOL || 'jsonrpc').trim().toLowerCase();
40
- if (!['jsonrpc', 'xmlrpc'].includes(protocol)) throw new Error(`BUGZILLA_PROTOCOL 取值非法:${protocol},可选 jsonrpc / xmlrpc`);
41
- const rpcPath = `/${(env.BUGZILLA_RPC_PATH || `${protocol}.cgi`).trim().replace(/^\/+/, '')}`;
39
+ // auto(默认):先用 JSON-RPC,服务端脚本出错(如 Not a HASH reference)时自动切换到 XML-RPC
40
+ const protocol = (env.BUGZILLA_PROTOCOL || 'auto').trim().toLowerCase();
41
+ if (!['auto', 'jsonrpc', 'xmlrpc'].includes(protocol)) throw new Error(`BUGZILLA_PROTOCOL 取值非法:${protocol},可选 auto / jsonrpc / xmlrpc`);
42
+ const rpcPathExplicit = Boolean((env.BUGZILLA_RPC_PATH || '').trim());
43
+ const rpcPath = `/${(env.BUGZILLA_RPC_PATH || `${protocol === 'xmlrpc' ? 'xmlrpc' : 'jsonrpc'}.cgi`).trim().replace(/^\/+/, '')}`;
42
44
  if (!/^\/[\w./-]+$/.test(rpcPath)) throw new Error(`BUGZILLA_RPC_PATH 非法:${rpcPath}`);
43
45
 
44
46
  const login = (env.BUGZILLA_LOGIN || '').trim();
@@ -62,6 +64,7 @@ export function loadConfig(env = process.env) {
62
64
  baseUrl: rawUrl,
63
65
  protocol,
64
66
  rpcPath,
67
+ rpcPathExplicit,
65
68
  authType,
66
69
  login,
67
70
  password,
@@ -28,7 +28,10 @@ export class BugzillaRpcClient {
28
28
  if (typeof fetchImpl !== 'function') throw new Error('当前 Node 版本不支持 fetch,请升级到 Node 18.17+');
29
29
  this.config = config;
30
30
  this.fetch = fetchImpl;
31
- this.endpoint = `${config.baseUrl}${config.rpcPath}`;
31
+ // auto 模式先走 JSON-RPC,遇到服务端 Perl 运行时错误或 HTML 页面时永久切换到 XML-RPC
32
+ this.protocol = config.protocol === 'xmlrpc' ? 'xmlrpc' : 'jsonrpc';
33
+ this.autoFallback = config.protocol === 'auto';
34
+ this.endpoint = this.endpointFor(this.protocol);
32
35
  this.cookies = new Map();
33
36
  this.loggedIn = false;
34
37
  this.userId = undefined;
@@ -57,6 +60,15 @@ export class BugzillaRpcClient {
57
60
  try {
58
61
  return await this.send(method, p);
59
62
  } catch (e) {
63
+ if (this.autoFallback && this.protocol === 'jsonrpc' && e.serverBroken) {
64
+ this.switchToXmlRpc(e);
65
+ try {
66
+ return await this.call(method, params, { skipAuth, retryAuth });
67
+ } catch (e2) {
68
+ e2.message = `JSON-RPC 与 XML-RPC 均失败。JSON-RPC:${e.message};XML-RPC:${e2.message}。WebService 接口在服务端不可用,需要 Bugzilla 管理员检查 jsonrpc.cgi / xmlrpc.cgi 的 Perl 依赖(JSON::RPC、SOAP::Lite)`;
69
+ throw e2;
70
+ }
71
+ }
60
72
  if (authType === 'cookie' && retryAuth && !skipAuth && RELOGIN_CODES.has(e.code)) {
61
73
  this.loggedIn = false;
62
74
  this.token = undefined;
@@ -69,7 +81,7 @@ export class BugzillaRpcClient {
69
81
  }
70
82
 
71
83
  async send(method, params) {
72
- const isJson = this.config.protocol === 'jsonrpc';
84
+ const isJson = this.protocol === 'jsonrpc';
73
85
  const body = isJson ? JSON.stringify({ version: '1.1', method, params: [params], id: ++this.seq }) : encodeCall(method, params);
74
86
  const canRetry = READ_METHODS.has(method);
75
87
  let attempt = 0;
@@ -120,6 +132,22 @@ export class BugzillaRpcClient {
120
132
  }
121
133
  }
122
134
 
135
+ endpointFor(protocol) {
136
+ const path = this.config.rpcPathExplicit ? this.config.rpcPath : `/${protocol}.cgi`;
137
+ return `${this.config.baseUrl}${path}`;
138
+ }
139
+
140
+ switchToXmlRpc(cause) {
141
+ this.protocol = 'xmlrpc';
142
+ this.autoFallback = false;
143
+ this.endpoint = this.endpointFor('xmlrpc');
144
+ // 会话 Cookie 与协议无关,但登录可能因 JSON-RPC 故障未成功,重置后用 XML-RPC 重新登录
145
+ this.loggedIn = false;
146
+ this.token = undefined;
147
+ this.fallbackReason = cause.message;
148
+ this.onProtocolSwitch?.(cause);
149
+ }
150
+
123
151
  parseJson(text, res) {
124
152
  let data;
125
153
  try {
@@ -128,9 +156,15 @@ export class BugzillaRpcClient {
128
156
  const hint = /<html/i.test(text)
129
157
  ? '返回的是 HTML 页面,服务器可能未安装 JSON-RPC 依赖(JSON::RPC 模块),可改用 BUGZILLA_PROTOCOL=xmlrpc'
130
158
  : `无法解析响应:${text.slice(0, 120)}`;
131
- throw new BugzillaError(`JSON-RPC 调用失败(HTTP ${res.status}):${hint}`, { status: res.status });
159
+ const err = new BugzillaError(`JSON-RPC 调用失败(HTTP ${res.status}):${hint}`, { status: res.status });
160
+ err.serverBroken = /<html/i.test(text) || isPerlRuntimeError(text);
161
+ throw err;
162
+ }
163
+ if (data?.error) {
164
+ const err = toError(data.error.code, data.error.message, res.status);
165
+ err.serverBroken = isPerlRuntimeError(data.error.message);
166
+ throw err;
132
167
  }
133
- if (data?.error) throw toError(data.error.code, data.error.message, res.status);
134
168
  return data?.result ?? null;
135
169
  }
136
170
 
@@ -250,6 +284,11 @@ export class BugzillaRpcClient {
250
284
  }
251
285
  }
252
286
 
287
+ /** Bugzilla 服务端 Perl 代码崩溃(如 JSON::RPC 模块版本与 Bugzilla 4.0 不兼容导致的 Not a HASH reference) */
288
+ export function isPerlRuntimeError(message) {
289
+ return /Not an? (HASH|ARRAY|CODE|SCALAR) reference|Can't (locate|call method|use)|at \S+\.pm line \d+|at \S+\.cgi line \d+/i.test(String(message || ''));
290
+ }
291
+
253
292
  function toError(code, message, status) {
254
293
  const c = typeof code === 'string' && /^\d+$/.test(code) ? Number(code) : code;
255
294
  const msg = String(message || '');
@@ -260,6 +299,7 @@ function toError(code, message, status) {
260
299
  else if (c === 101 || c === 100) text = `Bug 不存在(${c}):${msg}`;
261
300
  else if (c === 102) text = `无权查看该 Bug(102):${msg}`;
262
301
  else if (c === 32601 || c === -32601) text = `服务器不支持该方法(${c}),可能是 Bugzilla 版本过旧:${msg}`;
302
+ else if (isPerlRuntimeError(msg)) text = `Bugzilla 服务端脚本运行出错(code ${c ?? '未知'}):${msg}。这是服务器侧问题,常见原因是 JSON-RPC 依赖的 Perl 模块 JSON::RPC 版本与 Bugzilla 4.0 不兼容,可设置 BUGZILLA_PROTOCOL=xmlrpc`;
263
303
  else text = `Bugzilla 返回错误(code ${c ?? '未知'}):${msg}`;
264
304
  return new BugzillaError(text, { status, code: c });
265
305
  }
package/src/serve.js CHANGED
@@ -19,6 +19,7 @@ export async function serve(env) {
19
19
  try {
20
20
  config = loadConfig(env);
21
21
  client = new BugzillaRpcClient(config);
22
+ client.onProtocolSwitch = (e) => log(`JSON-RPC 服务端出错,已自动切换到 XML-RPC(${client.endpoint}):${e.message}`);
22
23
  log(`v${PACKAGE.version} 配置加载完成:${JSON.stringify(describeConfig(config))}`);
23
24
  } catch (e) {
24
25
  configError = e.message;
package/src/tools/meta.js CHANGED
@@ -12,7 +12,9 @@ export const verifyAuth = {
12
12
  const version = await client.version().then((r) => r?.version).catch((e) => `获取失败:${e.message}`);
13
13
  const base = {
14
14
  endpoint: client.endpoint,
15
- protocol: config.protocol,
15
+ protocol_setting: config.protocol,
16
+ protocol_in_use: client.protocol,
17
+ protocol_fallback_reason: client.fallbackReason,
16
18
  auth_type: config.authType,
17
19
  server_version: version,
18
20
  read_only: config.readOnly,