chaimi-bookkeeping-mcp 2.1.1 → 2.1.2

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.
File without changes
package/oauth.js CHANGED
@@ -52,9 +52,12 @@ class OAuthManager {
52
52
  */
53
53
  async startAuthFlow() {
54
54
  try {
55
- console.log('\n========================================');
56
- console.log('🔐 启动 OAuth 授权流程');
57
- console.log('========================================\n');
55
+ console.log('\n');
56
+ console.log('╔════════════════════════════════════════════════════════╗');
57
+ console.log('║ 🔴 【重要】首次使用需要完成 OAuth 授权 ║');
58
+ console.log('║ ⏱️ 授权流程约需 1-2 分钟,请耐心等待 ║');
59
+ console.log('╚════════════════════════════════════════════════════════╝');
60
+ console.log('\n');
58
61
 
59
62
  // 检测环境
60
63
  const env = await this.detectEnvironment();
@@ -65,7 +68,11 @@ class OAuthManager {
65
68
  const deviceCodeRes = await this.requestDeviceCode(useUrlScheme);
66
69
 
67
70
  console.log('✅ 获取设备码成功');
68
- console.log(`用户验证码: ${deviceCodeRes.userCode}`);
71
+ console.log('');
72
+ console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
73
+ console.log(` 📱 验证码: ${deviceCodeRes.userCode}`);
74
+ console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
75
+ console.log('');
69
76
 
70
77
  // 2. 根据环境选择交互方式
71
78
  if (useUrlScheme && deviceCodeRes.urlScheme) {
@@ -77,7 +84,10 @@ class OAuthManager {
77
84
  }
78
85
 
79
86
  // 3. 轮询获取 Token
80
- console.log('\n⏳ 等待用户授权...');
87
+ console.log('');
88
+ console.log('⏳ 等待用户授权,请勿关闭窗口...');
89
+ console.log(' (请在手机微信中完成授权操作)');
90
+ console.log('');
81
91
  const token = await this.pollForToken(
82
92
  deviceCodeRes.deviceCode,
83
93
  deviceCodeRes.interval * 1000 || 5000
@@ -86,8 +96,11 @@ class OAuthManager {
86
96
  // 4. 存储 Token
87
97
  await this.tokenStorage.save(token);
88
98
 
89
- console.log('\n✅ 授权成功!');
90
- console.log('========================================\n');
99
+ console.log('');
100
+ console.log('╔════════════════════════════════════════════════════════╗');
101
+ console.log('║ ✅ 授权成功!现在可以开始使用柴米记账 MCP Server ║');
102
+ console.log('╚════════════════════════════════════════════════════════╝');
103
+ console.log('');
91
104
 
92
105
  if (this.onTokenReady) {
93
106
  this.onTokenReady(token);
@@ -103,34 +116,89 @@ class OAuthManager {
103
116
 
104
117
  /**
105
118
  * 使用 URL Scheme 方式授权(浏览器环境)
119
+ * PC端:显示二维码 + 验证码
120
+ * 手机端:打开跳转链接
106
121
  */
107
122
  async authorizeWithUrlScheme(deviceCodeRes) {
108
- console.log('\n📱 请在浏览器中完成授权');
109
- console.log('正在尝试自动打开授权页面...\n');
123
+ // 检测是否为移动端环境
124
+ const isMobile = this.detectMobileEnvironment();
110
125
 
111
- // 显示完整的授权信息(用于小程序编译模式)
112
- console.log('========================================');
113
- console.log('小程序编译模式参数:');
114
- console.log('========================================');
115
- console.log(`启动页面: pages/agent-oauth/index`);
116
- console.log(`启动参数: deviceCode=${deviceCodeRes.deviceCode}&userCode=${deviceCodeRes.userCode}`);
117
- console.log('========================================\n');
126
+ if (isMobile) {
127
+ // 手机端:打开 URL Scheme 跳转链接
128
+ console.log('\n📱 请在微信中完成授权');
129
+ console.log('正在尝试自动打开微信...\n');
118
130
 
119
- const url = deviceCodeRes.urlScheme || deviceCodeRes.verificationUriFull || deviceCodeRes.verificationUri;
131
+ const url = deviceCodeRes.urlScheme || deviceCodeRes.verificationUriFull;
120
132
 
121
- try {
122
- // 尝试自动打开浏览器
123
- await this.openBrowser(url);
124
- console.log('✅ 已自动打开授权页面');
125
- console.log('请在打开的页面中完成授权\n');
126
- } catch (err) {
127
- // 自动打开失败,显示链接让用户手动点击
128
- console.log('⚠️ 无法自动打开浏览器,请手动访问以下链接:');
129
- console.log(`\n${url}\n`);
133
+ try {
134
+ await this.openBrowser(url);
135
+ console.log('✅ 已尝试打开微信');
136
+ console.log('请在微信中完成授权\n');
137
+ } catch (err) {
138
+ console.log('⚠️ 无法自动打开微信,请手动操作:');
139
+ console.log(`1. 复制链接: ${url}`);
140
+ console.log('2. 在微信中打开该链接\n');
141
+ }
142
+ } else {
143
+ // PC端:显示二维码 + 验证码
144
+ console.log('\n💻 请在手机微信中完成授权');
145
+ console.log('========================================');
146
+ console.log('');
147
+
148
+ // 生成小程序二维码链接
149
+ const qrCodeUrl = `https://mcp.chaihuo.com/auth?deviceCode=${deviceCodeRes.deviceCode}&userCode=${deviceCodeRes.userCode}`;
150
+
151
+ // 显示二维码(使用 ASCII 艺术或链接)
152
+ console.log('请使用微信扫描下方二维码:');
153
+ console.log('');
154
+ console.log(` ${qrCodeUrl}`);
155
+ console.log('');
156
+
157
+ // 如果有二维码回调,显示图形二维码
158
+ if (this.onQrCode) {
159
+ await this.onQrCode({
160
+ userCode: deviceCodeRes.userCode,
161
+ verificationUri: deviceCodeRes.verificationUri,
162
+ deviceCode: deviceCodeRes.deviceCode,
163
+ qrCodeUrl: qrCodeUrl,
164
+ isPC: true
165
+ });
166
+ }
167
+
168
+ console.log('========================================');
169
+ console.log('');
170
+ console.log('或者手动输入验证码:');
171
+ console.log(` 验证码: ${deviceCodeRes.userCode}`);
172
+ console.log('');
173
+ console.log('操作步骤:');
174
+ console.log('1. 打开微信,进入"柴米记账"小程序');
175
+ console.log('2. 点击"我的" → "🤖 Agent 授权"');
176
+ console.log(`3. 输入验证码: ${deviceCodeRes.userCode}`);
177
+ console.log('4. 点击"确认授权"');
178
+ console.log('');
179
+ }
180
+ }
181
+
182
+ /**
183
+ * 检测是否为移动端环境
184
+ */
185
+ detectMobileEnvironment() {
186
+ // 检测 User Agent
187
+ const userAgent = process.env.USER_AGENT || '';
188
+ if (/iPhone|iPad|iPod|Android|Mobile/i.test(userAgent)) {
189
+ return true;
190
+ }
191
+
192
+ // 检测是否有移动端特定的环境变量
193
+ if (process.env.TERM_PROGRAM === 'Apple_Terminal' && process.platform === 'darwin') {
194
+ // macOS 终端,可能是 iTerm2 等,需要进一步判断
195
+ // 这里假设 PC 端
196
+ return false;
130
197
  }
131
198
 
132
- // 显示备用验证码(如果自动打开失败)
133
- console.log(`备用验证码(如需手动输入): ${deviceCodeRes.userCode}`);
199
+ // 默认认为是 PC 端(保守策略)
200
+ // 因为 PC 端显示二维码是通用的,手机端打开链接需要确认是移动端
201
+ return false;
134
202
  }
135
203
 
136
204
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chaimi-bookkeeping-mcp",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "柴米记账 MCP Server - 支持 Claude、Cursor、OpenClaw、WorkBuddy 等 AI 工具直接记账",
5
5
  "main": "server.js",
6
6
  "bin": {