@@ -5,15 +5,6 @@ import {
55 CallToolRequestSchema ,
66 ListToolsRequestSchema ,
77} from '@modelcontextprotocol/sdk/types.js'
8- import {
9- ChannelPermissionRequestNotificationSchema ,
10- type ChannelPermissionRequestParams ,
11- } from '../mcp/channelNotification.js'
12- import { initializeAnalyticsSink } from '../analytics/sink.js'
13- import { shutdownDatadog } from '../analytics/datadog.js'
14- import { shutdown1PEventLogging } from '../analytics/firstPartyEventLogger.js'
15- import { enableConfigs } from '../../utils/config.js'
16- import { logForDebugging } from '../../utils/debug.js'
178import {
189 CDN_BASE_URL ,
1910 DEFAULT_BASE_URL ,
@@ -27,8 +18,21 @@ import {
2718 sendMediaFile ,
2819 sendText ,
2920 TypingStatus ,
30- } from '@claude-code-best/weixin'
31- import type { ParsedMessage } from '@claude-code-best/weixin'
21+ } from './index.js'
22+ import type { ParsedMessage } from './monitor.js'
23+ import type { ChannelPermissionRequestParams } from './permissions.js'
24+
25+ export interface WeixinServerDeps {
26+ enableConfigs ( ) : void
27+ initializeAnalyticsSink ( ) : void
28+ shutdownDatadog ( ) : Promise < void >
29+ shutdown1PEventLogging ( ) : Promise < void >
30+ logForDebugging ( message : string ) : void
31+ registerPermissionHandler (
32+ server : Server ,
33+ handler : ( request : ChannelPermissionRequestParams ) => Promise < void > ,
34+ ) : void
35+ }
3236
3337function formatPermissionRequestMessage (
3438 request : ChannelPermissionRequestParams ,
@@ -45,9 +49,9 @@ function formatPermissionRequestMessage(
4549 ] . join ( '\n' )
4650}
4751
48- export function createWeixinMcpServer ( ) : Server {
52+ export function createWeixinMcpServer ( version : string ) : Server {
4953 const server = new Server (
50- { name : 'weixin' , version : MACRO . VERSION } ,
54+ { name : 'weixin' , version } ,
5155 {
5256 capabilities : {
5357 experimental : {
@@ -224,61 +228,60 @@ export function createWeixinMcpServer(): Server {
224228 return server
225229}
226230
227- export async function runWeixinMcpServer ( ) : Promise < void > {
228- enableConfigs ( )
229- initializeAnalyticsSink ( )
231+ export async function runWeixinMcpServer (
232+ version : string ,
233+ deps : WeixinServerDeps ,
234+ ) : Promise < void > {
235+ deps . enableConfigs ( )
236+ deps . initializeAnalyticsSink ( )
230237
231238 const account = loadAccount ( )
232239 if ( ! account ) {
233240 process . stderr . write (
234241 '[weixin] No account configured. Run `ccb weixin login` to connect your WeChat account.\n' ,
235242 )
236- await Promise . all ( [ shutdown1PEventLogging ( ) , shutdownDatadog ( ) ] )
243+ await Promise . all ( [ deps . shutdown1PEventLogging ( ) , deps . shutdownDatadog ( ) ] )
237244 process . exit ( 1 )
238245 }
239246
240- const server = createWeixinMcpServer ( )
247+ const server = createWeixinMcpServer ( version )
241248 const transport = new StdioServerTransport ( )
242249
243- server . setNotificationHandler (
244- ChannelPermissionRequestNotificationSchema ( ) ,
245- async notification => {
246- const request = notification . params
247- const targetChatId = request . channel_context ?. chat_id
248- const targetChat = targetChatId
249- ? {
250- chatId : targetChatId ,
251- contextToken : getContextToken ( targetChatId ) ,
252- }
253- : getActivePermissionChat ( )
250+ deps . registerPermissionHandler ( server , async request => {
251+ const targetChatId = request . channel_context ?. chat_id
252+ const targetChat = targetChatId
253+ ? {
254+ chatId : targetChatId ,
255+ contextToken : getContextToken ( targetChatId ) ,
256+ }
257+ : getActivePermissionChat ( )
254258
255- if ( ! targetChat ) {
256- logForDebugging (
257- `[Weixin MCP] No active chat available for permission request ${ request . request_id } ` ,
258- )
259- return
260- }
259+ if ( ! targetChat ) {
260+ deps . logForDebugging (
261+ `[Weixin MCP] No active chat available for permission request ${ request . request_id } ` ,
262+ )
263+ return
264+ }
261265
262- try {
263- savePendingPermission (
264- request ,
265- targetChat . chatId ,
266- targetChat . contextToken ,
267- )
268- await sendText ( {
269- to : targetChat . chatId ,
270- text : formatPermissionRequestMessage ( request ) ,
271- baseUrl,
272- token : account . token ,
273- contextToken : targetChat . contextToken || '' ,
274- } )
275- } catch ( error ) {
276- process . stderr . write (
277- `[weixin] Failed to relay permission request ${ request . request_id } : ${ error } \n` ,
278- )
279- }
280- } ,
281- )
266+ try {
267+ savePendingPermission (
268+ request ,
269+ targetChat . chatId ,
270+ targetChat . contextToken ,
271+ )
272+ await sendText ( {
273+ to : targetChat . chatId ,
274+ text : formatPermissionRequestMessage ( request ) ,
275+ baseUrl,
276+ token : account . token ,
277+ contextToken : targetChat . contextToken || '' ,
278+ } )
279+ } catch ( error ) {
280+ process . stderr . write (
281+ `[weixin] Failed to relay permission request ${ request . request_id } : ${ error } \n` ,
282+ )
283+ }
284+ } )
282285
283286 await server . connect ( transport )
284287
@@ -292,7 +295,7 @@ export async function runWeixinMcpServer(): Promise<void> {
292295 if ( ! controller . signal . aborted ) {
293296 controller . abort ( )
294297 }
295- await Promise . all ( [ shutdown1PEventLogging ( ) , shutdownDatadog ( ) ] )
298+ await Promise . all ( [ deps . shutdown1PEventLogging ( ) , deps . shutdownDatadog ( ) ] )
296299 process . exit ( 0 )
297300 }
298301
@@ -313,7 +316,7 @@ export async function runWeixinMcpServer(): Promise<void> {
313316 }
314317 } , 5000 )
315318
316- logForDebugging ( '[Weixin MCP] Starting poll loop' )
319+ deps . logForDebugging ( '[Weixin MCP] Starting poll loop' )
317320 await startPollLoop ( {
318321 baseUrl,
319322 cdnBaseUrl : CDN_BASE_URL ,
0 commit comments