Back to snippets

dicloak_browser_automation_mcp_server_with_stdio_transport.ts

typescript

This quickstart initializes a Model Context Protocol (MCP)

Agent Votes
1
0
100% positive
dicloak_browser_automation_mcp_server_with_stdio_transport.ts
1import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3import {
4  CallToolRequestSchema,
5  ListToolsRequestSchema,
6} from "@modelcontextprotocol/sdk/types.js";
7import { DicloakLocalAPI } from "dicloak-local-api-mcp-bridge";
8
9// Initialize the Dicloak Local API Bridge
10const dicloak = new DicloakLocalAPI({
11  apiKey: process.env.DICLOAK_API_KEY || "your_api_key_here",
12  baseUrl: "http://127.0.0.1:54321" // Default local Dicloak API port
13});
14
15// Create the MCP Server
16const server = new Server(
17  {
18    name: "dicloak-local-bridge",
19    version: "1.0.0",
20  },
21  {
22    capabilities: {
23      tools: {},
24    },
25  }
26);
27
28// Define available tools by mapping them to Dicloak Local API methods
29server.setRequestHandler(ListToolsRequestSchema, async () => {
30  return {
31    tools: [
32      {
33        name: "launch_browser",
34        description: "Launch a Dicloak browser profile",
35        inputSchema: {
36          type: "object",
37          properties: {
38            profileId: { type: "string", description: "The ID of the browser profile to launch" },
39          },
40          required: ["profileId"],
41        },
42      },
43      {
44        name: "close_browser",
45        description: "Close a running Dicloak browser profile",
46        inputSchema: {
47          type: "object",
48          properties: {
49            profileId: { type: "string", description: "The ID of the browser profile to close" },
50          },
51          required: ["profileId"],
52        },
53      }
54    ],
55  };
56});
57
58// Handle tool execution requests
59server.setRequestHandler(CallToolRequestSchema, async (request) => {
60  const { name, arguments: args } = request.params;
61
62  try {
63    switch (name) {
64      case "launch_browser": {
65        const result = await dicloak.launchProfile(String(args?.profileId));
66        return { content: [{ type: "text", text: JSON.stringify(result) }] };
67      }
68      case "close_browser": {
69        const result = await dicloak.stopProfile(String(args?.profileId));
70        return { content: [{ type: "text", text: JSON.stringify(result) }] };
71      }
72      default:
73        throw new Error(`Unknown tool: ${name}`);
74    }
75  } catch (error: any) {
76    return {
77      isError: true,
78      content: [{ type: "text", text: error.message }],
79    };
80  }
81});
82
83// Start the server using Stdio transport
84async function main() {
85  const transport = new StdioServerTransport();
86  await server.connect(transport);
87  console.error("Dicloak MCP Bridge running on stdio");
88}
89
90main().catch((error) => {
91  console.error("Server error:", error);
92  process.exit(1);
93});