appium-mcp 1.76.0 → 1.78.0

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +169 -70
  3. package/dist/cli/index.d.ts +2 -0
  4. package/dist/cli/index.d.ts.map +1 -0
  5. package/dist/cli/index.js +49 -0
  6. package/dist/cli/index.js.map +1 -0
  7. package/dist/command.js +1 -1
  8. package/dist/command.js.map +1 -1
  9. package/dist/core.d.ts +12 -0
  10. package/dist/core.d.ts.map +1 -0
  11. package/dist/core.js +10 -0
  12. package/dist/core.js.map +1 -0
  13. package/dist/create-server.d.ts +50 -0
  14. package/dist/create-server.d.ts.map +1 -0
  15. package/dist/create-server.js +180 -0
  16. package/dist/create-server.js.map +1 -0
  17. package/dist/index.js +2 -36
  18. package/dist/index.js.map +1 -1
  19. package/dist/plugin.d.ts +209 -0
  20. package/dist/plugin.d.ts.map +1 -0
  21. package/dist/plugin.js +367 -0
  22. package/dist/plugin.js.map +1 -0
  23. package/dist/server.d.ts +1 -2
  24. package/dist/server.d.ts.map +1 -1
  25. package/dist/server.js +2 -67
  26. package/dist/server.js.map +1 -1
  27. package/dist/session-store.d.ts +8 -8
  28. package/dist/session-store.d.ts.map +1 -1
  29. package/dist/session-store.js +3 -2
  30. package/dist/session-store.js.map +1 -1
  31. package/dist/tests/__mocks__/@appium/support.d.ts +54 -61
  32. package/dist/tests/__mocks__/@appium/support.d.ts.map +1 -1
  33. package/dist/tests/__mocks__/@appium/support.js +42 -70
  34. package/dist/tests/__mocks__/@appium/support.js.map +1 -1
  35. package/dist/tests/create-server.test.d.ts +2 -0
  36. package/dist/tests/create-server.test.d.ts.map +1 -0
  37. package/dist/tests/create-server.test.js +253 -0
  38. package/dist/tests/create-server.test.js.map +1 -0
  39. package/dist/tests/plugin.test.d.ts +2 -0
  40. package/dist/tests/plugin.test.d.ts.map +1 -0
  41. package/dist/tests/plugin.test.js +340 -0
  42. package/dist/tests/plugin.test.js.map +1 -0
  43. package/dist/tests/tools/llm-wording.test.js +4 -1
  44. package/dist/tests/tools/llm-wording.test.js.map +1 -1
  45. package/dist/tests/verify.test.d.ts +2 -0
  46. package/dist/tests/verify.test.d.ts.map +1 -0
  47. package/dist/tests/verify.test.js +133 -0
  48. package/dist/tests/verify.test.js.map +1 -0
  49. package/dist/tests/vision-finder.test.d.ts +1 -1
  50. package/dist/tests/vision-finder.test.js +24 -6
  51. package/dist/tests/vision-finder.test.js.map +1 -1
  52. package/package.json +13 -1
  53. package/scripts/verify-names.mjs +12 -0
  54. package/server.json +2 -2
  55. package/src/cli/index.ts +58 -0
  56. package/src/command.ts +1 -1
  57. package/src/core.ts +28 -0
  58. package/src/create-server.ts +252 -0
  59. package/src/index.ts +2 -42
  60. package/src/plugin.ts +557 -0
  61. package/src/resources/submodules.zip +0 -0
  62. package/src/server.ts +2 -87
  63. package/src/session-store.ts +12 -11
package/src/plugin.ts ADDED
@@ -0,0 +1,557 @@
1
+ /**
2
+ * Plugin system for Appium MCP.
3
+ *
4
+ * This module defines the `AppiumMcpPlugin` interface and related types, as well
5
+ * as the `PluginManager` class which handles plugin registration, lifecycle, and
6
+ * tool call interception.
7
+ */
8
+
9
+ import type {
10
+ ContentResult,
11
+ FastMCP,
12
+ FastMCPSessionAuth,
13
+ Tool,
14
+ ToolParameters,
15
+ } from 'fastmcp';
16
+ import {
17
+ getDriver,
18
+ getSessionId,
19
+ getSessionInfo,
20
+ listSessions,
21
+ } from './session-store.js';
22
+ import type { DriverInstance, SessionInfo } from './session-store.js';
23
+ import log from './logger.js';
24
+ import registerTools from './tools/index.js';
25
+
26
+ const CORE_SOURCE = 'appium-mcp core';
27
+
28
+ /**
29
+ * Context passed to plugin lifecycle methods.
30
+ *
31
+ * This is intentionally smaller than the underlying FastMCP server. Plugins
32
+ * should use `McpRegistry` during `register()` for MCP capabilities and
33
+ * `AppiumMcpCore` for Appium MCP state.
34
+ */
35
+ export interface PluginContext {
36
+ readonly core: AppiumMcpCore;
37
+ readonly plugins: ReadonlyMap<string, AppiumMcpPlugin>;
38
+ }
39
+
40
+ /**
41
+ * Session helpers available to call hooks.
42
+ */
43
+ export interface PluginSessionContext {
44
+ getSessionInfo(sessionId?: string): SessionInfo | null;
45
+ getSessionId(): string | null;
46
+ getDriver(sessionId?: string): DriverInstance | null;
47
+ listSessions(): ReturnType<typeof listSessions>;
48
+ }
49
+
50
+ /**
51
+ * Context passed to `beforeCall` and `afterCall` for each MCP tool execution.
52
+ */
53
+ export interface ToolCallContext {
54
+ readonly toolName: string;
55
+ readonly args: Readonly<Record<string, unknown>>;
56
+ readonly session: PluginSessionContext;
57
+ }
58
+
59
+ /**
60
+ * Tool result shape plugins may return to short-circuit or modify a tool call.
61
+ */
62
+ export interface ToolCallResult {
63
+ isError: boolean;
64
+ content: ContentResult['content'];
65
+ }
66
+
67
+ /**
68
+ * Extension point for composing app-specific behavior into Appium MCP.
69
+ */
70
+ export interface AppiumMcpPlugin {
71
+ /**
72
+ * Unique plugin identifier within a server instance.
73
+ *
74
+ * Duplicate plugin names are skipped with a warning, so prefer stable
75
+ * package-style or organization-prefixed names.
76
+ */
77
+ readonly name: string;
78
+ readonly version: string;
79
+ initialize?(ctx: PluginContext): Promise<void>;
80
+ register?(registry: McpRegistry, core: AppiumMcpCore): void;
81
+ beforeCall?(ctx: ToolCallContext): Promise<ToolCallResult | void>;
82
+ afterCall?(
83
+ ctx: ToolCallContext,
84
+ result: ToolCallResult
85
+ ): Promise<ToolCallResult | void>;
86
+ destroy?(): Promise<void>;
87
+ }
88
+
89
+ export type VerificationDuplicateKind = 'plugin' | 'tool';
90
+
91
+ export interface VerificationEntry {
92
+ name: string;
93
+ source: string;
94
+ }
95
+
96
+ export interface VerificationDuplicate {
97
+ kind: VerificationDuplicateKind;
98
+ name: string;
99
+ entries: VerificationEntry[];
100
+ }
101
+
102
+ export interface VerificationError {
103
+ source: string;
104
+ message: string;
105
+ }
106
+
107
+ export interface VerificationReport {
108
+ ok: boolean;
109
+ pluginCount: number;
110
+ toolCount: number;
111
+ duplicates: VerificationDuplicate[];
112
+ errors: VerificationError[];
113
+ }
114
+
115
+ export interface VerifyAppiumMcpNamesOptions {
116
+ plugins?: AppiumMcpPlugin[];
117
+ errors?: VerificationError[];
118
+ }
119
+
120
+ type AddToolParam = Parameters<FastMCP['addTool']>[0];
121
+
122
+ type AddPromptParam = Parameters<FastMCP['addPrompt']>[0];
123
+
124
+ type AddResourceParam = Parameters<FastMCP['addResource']>[0];
125
+
126
+ type AddResourceTemplateParam = Parameters<FastMCP['addResourceTemplate']>[0];
127
+
128
+ type VerificationToolDef = {
129
+ name: string;
130
+ };
131
+
132
+ type CapabilityCollector = {
133
+ addTool(toolDef: VerificationToolDef): void;
134
+ addPrompt(promptDef: unknown): void;
135
+ addResource(resourceDef: unknown): void;
136
+ addResourceTemplate(resourceTemplateDef: unknown): void;
137
+ };
138
+
139
+ export class McpRegistry {
140
+ constructor(private readonly server: FastMCP) {}
141
+
142
+ /**
143
+ * Register one MCP tool. Tool calls are wrapped by plugin call hooks.
144
+ *
145
+ * Delegates to FastMCP `addTool`.
146
+ *
147
+ * @see https://github.com/punkpeye/fastmcp#tools
148
+ */
149
+ addTool<Params extends ToolParameters>(
150
+ name: string,
151
+ description: string,
152
+ parameters: Params,
153
+ execute: Tool<FastMCPSessionAuth, Params>['execute']
154
+ ): void {
155
+ this.server.addTool({ name, description, parameters, execute });
156
+ }
157
+
158
+ /**
159
+ * Register multiple MCP tools.
160
+ *
161
+ * Delegates to FastMCP `addTool` for each definition.
162
+ *
163
+ * @see https://github.com/punkpeye/fastmcp#tools
164
+ */
165
+ addTools(
166
+ defs: Array<{
167
+ name: string;
168
+ description: string;
169
+ parameters: ToolParameters;
170
+ execute: AddToolParam['execute'];
171
+ }>
172
+ ): void {
173
+ for (const def of defs) {
174
+ this.addTool(def.name, def.description, def.parameters, def.execute);
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Register one MCP prompt.
180
+ *
181
+ * Delegates to FastMCP `addPrompt`.
182
+ *
183
+ * @see https://github.com/punkpeye/fastmcp#prompts
184
+ */
185
+ addPrompt(prompt: AddPromptParam): void {
186
+ this.server.addPrompt(prompt);
187
+ }
188
+
189
+ /**
190
+ * Register multiple MCP prompts.
191
+ *
192
+ * Delegates to FastMCP `addPrompt` for each definition.
193
+ *
194
+ * @see https://github.com/punkpeye/fastmcp#prompts
195
+ */
196
+ addPrompts(prompts: AddPromptParam[]): void {
197
+ for (const prompt of prompts) {
198
+ this.addPrompt(prompt);
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Register one MCP resource.
204
+ *
205
+ * Delegates to FastMCP `addResource`.
206
+ *
207
+ * @see https://github.com/punkpeye/fastmcp#resources
208
+ */
209
+ addResource(resource: AddResourceParam): void {
210
+ this.server.addResource(resource);
211
+ }
212
+
213
+ /**
214
+ * Register multiple MCP resources.
215
+ *
216
+ * Delegates to FastMCP `addResource` for each definition.
217
+ *
218
+ * @see https://github.com/punkpeye/fastmcp#resources
219
+ */
220
+ addResources(resources: AddResourceParam[]): void {
221
+ for (const resource of resources) {
222
+ this.addResource(resource);
223
+ }
224
+ }
225
+
226
+ /**
227
+ * Register one MCP resource template.
228
+ *
229
+ * Delegates to FastMCP `addResourceTemplate`.
230
+ *
231
+ * @see https://github.com/punkpeye/fastmcp#resource-templates
232
+ */
233
+ addResourceTemplate(resourceTemplate: AddResourceTemplateParam): void {
234
+ this.server.addResourceTemplate(resourceTemplate);
235
+ }
236
+
237
+ /**
238
+ * Register multiple MCP resource templates.
239
+ *
240
+ * Delegates to FastMCP `addResourceTemplate` for each definition.
241
+ *
242
+ * @see https://github.com/punkpeye/fastmcp#resource-templates
243
+ */
244
+ addResourceTemplates(resourceTemplates: AddResourceTemplateParam[]): void {
245
+ for (const resourceTemplate of resourceTemplates) {
246
+ this.addResourceTemplate(resourceTemplate);
247
+ }
248
+ }
249
+ }
250
+
251
+ /**
252
+ * Safe Appium MCP primitives exposed to plugins.
253
+ */
254
+ export class AppiumMcpCore {
255
+ /**
256
+ * Return the currently active Appium session id, if one exists.
257
+ */
258
+ getSessionId(): string | null {
259
+ return getSessionId();
260
+ }
261
+
262
+ /**
263
+ * Return metadata for a specific session, or the active session if `sessionId` is not provided.
264
+ */
265
+ getSessionInfo(sessionId?: string): SessionInfo | null {
266
+ return getSessionInfo(sessionId);
267
+ }
268
+
269
+ /**
270
+ * Return the active driver, or a driver for a specific Appium session id.
271
+ */
272
+ getDriver(sessionId?: string): DriverInstance | null {
273
+ return getDriver(sessionId);
274
+ }
275
+
276
+ /**
277
+ * Return metadata for all Appium sessions tracked by this server.
278
+ */
279
+ listSessions(): ReturnType<typeof listSessions> {
280
+ return listSessions();
281
+ }
282
+ }
283
+
284
+ export class PluginManager {
285
+ private readonly pluginMap = new Map<string, AppiumMcpPlugin>();
286
+ private readonly server: FastMCP;
287
+ private readonly core: AppiumMcpCore;
288
+ private readonly capabilityPluginNames = new Set<string>();
289
+ private addToolInterceptorInstalled = false;
290
+
291
+ constructor(server: FastMCP) {
292
+ this.server = server;
293
+ this.core = new AppiumMcpCore();
294
+ }
295
+
296
+ register(plugins: AppiumMcpPlugin[]): void {
297
+ for (const plugin of plugins) {
298
+ if (this.pluginMap.has(plugin.name)) {
299
+ log.warn(
300
+ `[PluginManager] Duplicate plugin name "${plugin.name}" – skipping.`
301
+ );
302
+ continue;
303
+ }
304
+ this.pluginMap.set(plugin.name, plugin);
305
+ log.info(
306
+ `[PluginManager] Registered plugin "${plugin.name}" v${plugin.version}`
307
+ );
308
+ }
309
+ this.installAddToolInterceptor();
310
+ }
311
+
312
+ registerPluginCapabilities(): void {
313
+ const registry = new McpRegistry(this.server);
314
+ for (const plugin of this.pluginMap.values()) {
315
+ if (this.capabilityPluginNames.has(plugin.name)) {
316
+ log.warn(
317
+ `[PluginManager] Duplicate plugin name "${plugin.name}" – skipping.`
318
+ );
319
+ continue;
320
+ }
321
+ this.capabilityPluginNames.add(plugin.name);
322
+
323
+ if (typeof plugin.register === 'function') {
324
+ plugin.register(registry, this.core);
325
+ }
326
+ }
327
+ }
328
+
329
+ async initialize(): Promise<void> {
330
+ const ctx: PluginContext = {
331
+ core: this.core,
332
+ plugins: this.pluginMap as ReadonlyMap<string, AppiumMcpPlugin>,
333
+ };
334
+ for (const plugin of this.pluginMap.values()) {
335
+ if (typeof plugin.initialize === 'function') {
336
+ await plugin.initialize(ctx);
337
+ }
338
+ }
339
+ }
340
+
341
+ async destroy(): Promise<void> {
342
+ for (const plugin of Array.from(this.pluginMap.values()).reverse()) {
343
+ if (typeof plugin.destroy === 'function') {
344
+ await plugin.destroy();
345
+ }
346
+ }
347
+ }
348
+
349
+ private installAddToolInterceptor(): void {
350
+ if (this.addToolInterceptorInstalled) {
351
+ return;
352
+ }
353
+ this.addToolInterceptorInstalled = true;
354
+
355
+ const originalAddTool = this.server.addTool.bind(this.server);
356
+
357
+ this.server.addTool = (toolDef: AddToolParam): void => {
358
+ const wrappedExecute: AddToolParam['execute'] = async (args, mcpCtx) => {
359
+ const sessionCtx: PluginSessionContext = {
360
+ getSessionId: () => getSessionId(),
361
+ getSessionInfo: (sessionId?: string) => getSessionInfo(sessionId),
362
+ getDriver: (sessionId?: string) => getDriver(sessionId),
363
+ listSessions,
364
+ };
365
+
366
+ const toolCtx: ToolCallContext = {
367
+ toolName: toolDef.name,
368
+ args: (args || {}) as Record<string, unknown>,
369
+ session: sessionCtx,
370
+ };
371
+
372
+ for (const plugin of this.pluginMap.values()) {
373
+ if (typeof plugin.beforeCall !== 'function') {
374
+ continue;
375
+ }
376
+ const override = await plugin.beforeCall(toolCtx);
377
+ if (override != null) {
378
+ return {
379
+ content: override.content,
380
+ isError: override.isError,
381
+ } as ContentResult;
382
+ }
383
+ }
384
+
385
+ const rawResult = (await toolDef.execute(
386
+ args,
387
+ mcpCtx
388
+ )) as ContentResult;
389
+ let hookResult: ToolCallResult = {
390
+ isError: rawResult.isError ?? false,
391
+ content: rawResult.content as ToolCallResult['content'],
392
+ };
393
+
394
+ for (const plugin of this.pluginMap.values()) {
395
+ if (typeof plugin.afterCall !== 'function') {
396
+ continue;
397
+ }
398
+ const modified = await plugin.afterCall(toolCtx, hookResult);
399
+ if (modified != null) {
400
+ hookResult = modified;
401
+ }
402
+ }
403
+
404
+ return {
405
+ content: hookResult.content,
406
+ isError: hookResult.isError,
407
+ } as ContentResult;
408
+ };
409
+
410
+ return originalAddTool({ ...toolDef, execute: wrappedExecute });
411
+ };
412
+ }
413
+ }
414
+
415
+ /**
416
+ * Verify that plugin and tool names are unique across a set of plugins
417
+ * and report any duplicates or registration errors.
418
+ * @param options - Options for verification, including the list of plugins and any pre-existing errors.
419
+ * @returns A report detailing any duplicates or errors found during verification.
420
+ */
421
+ export function verifyAppiumMcpNames(
422
+ options: VerifyAppiumMcpNamesOptions = {}
423
+ ): VerificationReport {
424
+ const plugins = options.plugins ?? [];
425
+ const errors = [...(options.errors ?? [])];
426
+ const duplicates: VerificationDuplicate[] = [];
427
+ const toolEntries: VerificationEntry[] = [];
428
+ let currentSource = CORE_SOURCE;
429
+
430
+ const collector: CapabilityCollector = {
431
+ addTool(toolDef: VerificationToolDef) {
432
+ toolEntries.push({
433
+ name: toolDef.name,
434
+ source: currentSource,
435
+ });
436
+ },
437
+ addPrompt() {},
438
+ addResource() {},
439
+ addResourceTemplate() {},
440
+ };
441
+
442
+ const pluginEntries = plugins.map((plugin) => ({
443
+ name: plugin.name,
444
+ source: `plugin:${plugin.name}@${plugin.version}`,
445
+ }));
446
+ duplicates.push(...findDuplicates('plugin', pluginEntries));
447
+
448
+ const seenPluginNames = new Set<string>();
449
+ const registry = new McpRegistry(collector as never);
450
+ const core = new AppiumMcpCore();
451
+
452
+ for (const plugin of plugins) {
453
+ if (seenPluginNames.has(plugin.name)) {
454
+ continue;
455
+ }
456
+ seenPluginNames.add(plugin.name);
457
+ if (typeof plugin.register !== 'function') {
458
+ continue;
459
+ }
460
+ currentSource = `plugin:${plugin.name}`;
461
+ try {
462
+ plugin.register(registry, core);
463
+ } catch (err: unknown) {
464
+ errors.push({
465
+ source: currentSource,
466
+ message: errorMessage(err),
467
+ });
468
+ }
469
+ }
470
+
471
+ currentSource = CORE_SOURCE;
472
+ try {
473
+ withSuppressedRegistrationLogs(() => registerTools(collector as never));
474
+ } catch (err: unknown) {
475
+ errors.push({
476
+ source: currentSource,
477
+ message: errorMessage(err),
478
+ });
479
+ }
480
+ duplicates.push(...findDuplicates('tool', toolEntries));
481
+
482
+ return {
483
+ ok: duplicates.length === 0 && errors.length === 0,
484
+ pluginCount: new Set(pluginEntries.map((entry) => entry.name)).size,
485
+ toolCount: toolEntries.length,
486
+ duplicates,
487
+ errors,
488
+ };
489
+ }
490
+
491
+ export function formatVerificationReport(report: VerificationReport): string {
492
+ const lines = [
493
+ `Checked ${report.pluginCount} plugin name(s) and ${report.toolCount} tool name(s).`,
494
+ ];
495
+
496
+ if (report.ok) {
497
+ lines.push('No duplicate plugin or tool names found.');
498
+ return lines.join('\n');
499
+ }
500
+
501
+ if (report.duplicates.length > 0) {
502
+ lines.push('Duplicate names found:');
503
+ for (const duplicate of report.duplicates) {
504
+ const sources = duplicate.entries
505
+ .map((entry) => ` - ${entry.source}`)
506
+ .join('\n');
507
+ lines.push(` ${duplicate.kind}: ${duplicate.name}\n${sources}`);
508
+ }
509
+ }
510
+
511
+ if (report.errors.length > 0) {
512
+ lines.push('Registration/load errors found:');
513
+ for (const error of report.errors) {
514
+ lines.push(` ${error.source}: ${error.message}`);
515
+ }
516
+ }
517
+
518
+ return lines.join('\n');
519
+ }
520
+
521
+ function withSuppressedRegistrationLogs(fn: () => void): void {
522
+ const mutableLog = log as typeof log & { info: (...args: unknown[]) => void };
523
+ const originalInfo = mutableLog.info;
524
+ mutableLog.info = () => {};
525
+ try {
526
+ fn();
527
+ } finally {
528
+ mutableLog.info = originalInfo;
529
+ }
530
+ }
531
+
532
+ function findDuplicates(
533
+ kind: VerificationDuplicateKind,
534
+ entries: VerificationEntry[]
535
+ ): VerificationDuplicate[] {
536
+ const byName = new Map<string, VerificationEntry[]>();
537
+ for (const entry of entries) {
538
+ const existing = byName.get(entry.name) ?? [];
539
+ existing.push(entry);
540
+ byName.set(entry.name, existing);
541
+ }
542
+
543
+ return Array.from(byName.entries())
544
+ .filter(([, duplicateEntries]) => duplicateEntries.length > 1)
545
+ .map(([name, duplicateEntries]) => ({
546
+ kind,
547
+ name,
548
+ entries: duplicateEntries,
549
+ }));
550
+ }
551
+
552
+ function errorMessage(err: unknown): string {
553
+ if (err instanceof Error) {
554
+ return err.message;
555
+ }
556
+ return String(err);
557
+ }
Binary file
package/src/server.ts CHANGED
@@ -1,89 +1,4 @@
1
- import { FastMCP } from 'fastmcp';
2
- import pkg from '../package.json' with { type: 'json' };
3
- import registerTools from './tools/index.js';
4
- import registerResources from './resources/index.js';
5
- import { listSessions, safeDeleteAllSessions } from './session-store.js';
6
- import log from './logger.js';
7
-
8
- // FastMCP types `version` as a literal `${number}.${number}.${number}` template,
9
- // while `package.json.version` is just `string`. The cast is the supported
10
- // escape hatch for projects that want the published version to flow through.
11
- const SERVER_VERSION = pkg.version as `${number}.${number}.${number}`;
12
-
13
- const SERVER_INSTRUCTIONS = [
14
- 'Appium mobile automation through MCP. Defaults that avoid broken flows:',
15
- '- Establish a driver session first: select_device and appium_session_management (action=create) for local/embedded mode, or attach to a remote session when the user supplies a server URL.',
16
- '- Call only tools this server actually registers (appium_find_element, appium_gesture, appium_session_management, etc.); do not invent tool names or aliases.',
17
- '- Prefer stable locators: accessibility id and id before long xpath; use xpath only when nothing else works.',
18
- '- Use appium_gesture for taps and drags; when something is off-screen, use action=scroll_to_element instead of spamming appium_find_element alone.',
19
- '- For local Appium install, doctor, or smoke tests, run appium_skills before guessing commands.',
20
- ].join('\n');
21
-
22
- type DisconnectSessionPolicy = 'delete_all' | 'skip';
23
-
24
- /**
25
- * MCP disconnect policy for Appium sessions tracked by this server.
26
- * - delete_all (default): end every owned session when the MCP client disconnects (avoids leaked drivers).
27
- * - skip: keep sessions across disconnects — needed for flaky HTTP/stream clients that reconnect briefly.
28
- */
29
- function disconnectSessionPolicyFromEnv(): DisconnectSessionPolicy {
30
- const raw = process.env.APPIUM_MCP_ON_CLIENT_DISCONNECT?.trim().toLowerCase();
31
- if (raw === 'skip') {
32
- return 'skip';
33
- }
34
- if (raw !== 'delete_all') {
35
- log.warn(
36
- `APPIUM_MCP_ON_CLIENT_DISCONNECT="${raw}" is not recognized (expected delete_all or skip); defaulting to delete_all`
37
- );
38
- }
39
- return 'delete_all';
40
- }
41
-
42
- const server = new FastMCP({
43
- name: 'MCP Appium',
44
- version: SERVER_VERSION,
45
- instructions: SERVER_INSTRUCTIONS,
46
- });
47
-
48
- registerResources(server);
49
- registerTools(server);
50
-
51
- // Handle client connection and disconnection events
52
- server.on('connect', (event) => {
53
- log.info('Client connected:', event.session);
54
- });
55
-
56
- server.on('disconnect', async (event) => {
57
- log.info('Client disconnected:', event.session);
58
- const policy = disconnectSessionPolicyFromEnv();
59
-
60
- const ownedSessions = listSessions().filter(
61
- (session) => session.ownership === 'owned'
62
- );
63
-
64
- if (ownedSessions.length > 0 && policy === 'skip') {
65
- log.info(
66
- `${ownedSessions.length} owned session(s) retained after MCP disconnect ` +
67
- '(APPIUM_MCP_ON_CLIENT_DISCONNECT=skip). Delete explicitly via appium_session_management (action=delete) when finished.'
68
- );
69
- return;
70
- }
71
-
72
- if (ownedSessions.length > 0) {
73
- try {
74
- log.info(
75
- `${ownedSessions.length} owned session(s) detected on disconnect, cleaning up...`
76
- );
77
- const deletedCount = await safeDeleteAllSessions();
78
- log.info(
79
- `${deletedCount} session(s) cleaned up successfully on disconnect.`
80
- );
81
- } catch (error) {
82
- log.error('Error cleaning up session on disconnect:', error);
83
- }
84
- } else {
85
- log.info('No owned sessions to clean up on disconnect.');
86
- }
87
- });
1
+ import { createAppiumMcpServer } from './create-server.js';
88
2
 
3
+ const server = createAppiumMcpServer();
89
4
  export default server;
@@ -16,14 +16,7 @@ export type NullableDriverInstance = DriverInstance | null;
16
16
  export type SessionCapabilities = Record<string, any>;
17
17
  export type SessionOwnership = 'owned' | 'attached';
18
18
 
19
- interface SessionMetadata {
20
- platform: string | null;
21
- automationName: string | null;
22
- deviceName: string | null;
23
- capabilities: SessionCapabilities;
24
- }
25
-
26
- interface SessionInfo {
19
+ export interface SessionInfo {
27
20
  driver: DriverInstance;
28
21
  sessionId: string;
29
22
  currentContext: string | null;
@@ -33,6 +26,13 @@ interface SessionInfo {
33
26
  remoteServerUrl?: string;
34
27
  }
35
28
 
29
+ interface SessionMetadata {
30
+ platform: string | null;
31
+ automationName: string | null;
32
+ deviceName: string | null;
33
+ capabilities: SessionCapabilities;
34
+ }
35
+
36
36
  /**
37
37
  * In-memory store for active Appium sessions and their associated drivers.
38
38
  */
@@ -239,11 +239,12 @@ export function setCurrentContext(
239
239
  return true;
240
240
  }
241
241
 
242
- export function getSessionInfo(sessionId: string | null): SessionInfo | null {
243
- if (!sessionId) {
242
+ export function getSessionInfo(sessionId?: string): SessionInfo | null {
243
+ const id = sessionId ?? activeSessionId;
244
+ if (!id) {
244
245
  return null;
245
246
  }
246
- return sessions.get(sessionId) ?? null;
247
+ return sessions.get(id) ?? null;
247
248
  }
248
249
 
249
250
  export function getCurrentContext(sessionId?: string): string | null {