Skip to main content
Glama

press_key

Simulate keyboard input to automate interactions in Tauri desktop applications for testing and workflow automation.

Instructions

Press key

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
keyYesKey name
windowNoWindow label (default: focused window)

Implementation Reference

  • Schema definition for the press_key tool, specifying name, description, and input validation using Zod schema (key is required, window is optional)
    press_key: {
      name: 'press_key',
      description: 'Press key',
      inputSchema: z.object({
        key: z.string().describe('Key name'),
        window: z.string().optional().describe('Window label (default: focused window)'),
      }),
    },
  • MCP tool handler for press_key that receives arguments, calls socketManager.pressKey(), and formats the response in the MCP content format
    press_key: async (args: { key: string; window?: string }) => {
      const result = await socketManager.pressKey(args.key, args.window);
      return {
        content: [
          {
            type: 'text' as const,
            text: result,
          },
        ],
      };
    },
  • SocketManager method that sends the press_key command to the Tauri app via JSON-RPC over a Unix socket or named pipe, with error handling and retry logic
    async pressKey(key: string, windowLabel?: string): Promise<string> {
      const params: Record<string, unknown> = { key };
      if (windowLabel) params.window = windowLabel;
    
      const result = await this.sendCommand('press_key', params) as { success: boolean; error?: string };
      if (!result.success) {
        throw new Error(result.error || 'Press key failed');
      }
      const windowInfo = windowLabel ? ` in window '${windowLabel}'` : '';
      return `Pressed key: ${key}${windowInfo}`;
    }
  • Server registration code that registers all tools including press_key with the MCP server, converting Zod schemas to MCP tool definitions
    // List tools handler
    this.server.setRequestHandler(ListToolsRequestSchema, async () => {
      const allSchemas = Object.values(toolSchemas);
    
      // Filter tools if ESSENTIAL_TOOLS is set
      const filteredSchemas = essentialTools
        ? allSchemas.filter((schema) => essentialTools.has(schema.name))
        : allSchemas;
    
      const tools: Tool[] = filteredSchemas.map((schema) => {
        const properties: Record<string, object> = {};
        const required: string[] = [];
    
        const shape = schema.inputSchema.shape as Record<string, unknown>;
        for (const [key, zodValue] of Object.entries(shape)) {
          const zodSchema = zodValue as { _def?: { typeName?: string; description?: string }; description?: string; isOptional?: () => boolean };
          properties[key] = {
            type: this.getZodType(zodSchema),
            description: zodSchema._def?.description || zodSchema.description || '',
          };
    
          // Check if required (not optional)
          if (!zodSchema.isOptional?.()) {
            const typeName = zodSchema._def?.typeName;
            if (typeName !== 'ZodOptional') {
              required.push(key);
            }
          }
        }
    
        return {
          name: schema.name,
          description: schema.description,
          inputSchema: {
            type: 'object' as const,
            properties,
            required: required.length > 0 ? required : undefined,
          },
        };
      });
    
      return { tools };
    });
  • CallTool request handler that routes tool execution requests (including press_key) to their corresponding handlers and wraps errors in the MCP response format
    // Call tool handler
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
      const { name, arguments: args } = request.params;
    
      if (!(name in this.toolHandlers)) {
        throw new Error(`Unknown tool: ${name}`);
      }
    
      const handler = this.toolHandlers[name as ToolName];
    
      try {
        return await handler(args as never);
      } catch (error) {
        return {
          content: [
            {
              type: 'text' as const,
              text: `Error: ${(error as Error).message}`,
            },
          ],
          isError: true,
        };
      }
    });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/DaveDev42/tauri-plugin-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server