Skip to main content
Glama

List Workspaces

list_workspaces

Retrieve all available workspaces on the AFFiNE MCP Server to manage documents, search content, handle comments, and access version history efficiently.

Instructions

List all available AFFiNE workspaces

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The core handler function that implements the list_workspaces tool logic by querying the GraphQL endpoint for available workspaces and returning the list or an error.
    const listWorkspacesHandler = async () => {
      try {
        const query = `query { workspaces { id public enableAi createdAt } }`;
        const data = await gql.request<{ workspaces: any[] }>(query);
        return text(data.workspaces || []);
      } catch (error: any) {
        return text({ error: error.message });
      }
    };
  • Direct registration of the 'list_workspaces' tool with the MCP server, including metadata and handler reference.
    server.registerTool(
      "list_workspaces",
      {
        title: "List Workspaces",
        description: "List all available AFFiNE workspaces"
      },
      listWorkspacesHandler as any
    );
  • Additional registration of an alias 'affine_list_workspaces' using the same handler.
    server.registerTool(
      "affine_list_workspaces",
      {
        title: "List Workspaces",
        description: "List all available AFFiNE workspaces"
      },
      listWorkspacesHandler
    );
  • src/index.ts:65-65 (registration)
    Top-level call to register all workspace-related tools, including list_workspaces, during server initialization.
    registerWorkspaceTools(server, gql);
  • Helper function that defines and registers all workspace tools, including the listWorkspacesHandler and its registrations.
    export function registerWorkspaceTools(server: McpServer, gql: GraphQLClient) {
      // LIST WORKSPACES
      const listWorkspacesHandler = async () => {
        try {
          const query = `query { workspaces { id public enableAi createdAt } }`;
          const data = await gql.request<{ workspaces: any[] }>(query);
          return text(data.workspaces || []);
        } catch (error: any) {
          return text({ error: error.message });
        }
      };
    
      server.registerTool(
        "list_workspaces",
        {
          title: "List Workspaces",
          description: "List all available AFFiNE workspaces"
        },
        listWorkspacesHandler as any
      );
      server.registerTool(
        "affine_list_workspaces",
        {
          title: "List Workspaces",
          description: "List all available AFFiNE workspaces"
        },
        listWorkspacesHandler
      );
    
      // GET WORKSPACE
      const getWorkspaceHandler = async ({ id }: { id: string }) => {
        try {
          const query = `query GetWorkspace($id: String!) { 
            workspace(id: $id) { 
              id 
              public 
              enableAi 
              createdAt
              permissions { 
                Workspace_Read 
                Workspace_CreateDoc 
              } 
            } 
          }`;
          const data = await gql.request<{ workspace: any }>(query, { id });
          return text(data.workspace);
        } catch (error: any) {
          return text({ error: error.message });
        }
      };
    
      server.registerTool(
        "get_workspace",
        {
          title: "Get Workspace",
          description: "Get details of a specific workspace",
          inputSchema: { 
            id: z.string().describe("Workspace ID") 
          }
        },
        getWorkspaceHandler as any
      );
      server.registerTool(
        "affine_get_workspace",
        {
          title: "Get Workspace",
          description: "Get details of a specific workspace",
          inputSchema: { 
            id: z.string().describe("Workspace ID") 
          }
        },
        getWorkspaceHandler as any
      );
    
      // CREATE WORKSPACE
      const createWorkspaceHandler = async ({ name, avatar }: { name: string; avatar?: string }) => {
          try {
            // Get endpoint and headers from GraphQL client
            const endpoint = (gql as any).endpoint || process.env.AFFINE_BASE_URL + '/graphql';
            const headers = (gql as any).headers || {};
            const cookie = (gql as any).cookie || headers.Cookie || '';
            
            // Create initial workspace data
            const { workspaceUpdate, firstDocId, docUpdate } = createInitialWorkspaceData(name);
            
            // Only send workspace update - document will be created separately
            const initData = Buffer.from(workspaceUpdate);
            
            // Create multipart form
            const form = new FormData();
            
            // Add GraphQL operation
            form.append('operations', JSON.stringify({
              name: 'createWorkspace',
              query: `mutation createWorkspace($init: Upload!) {
                createWorkspace(init: $init) {
                  id
                  public
                  createdAt
                  enableAi
                }
              }`,
              variables: { init: null }
            }));
            
            // Map file to variable
            form.append('map', JSON.stringify({ '0': ['variables.init'] }));
            
            // Add workspace init data
            form.append('0', initData, {
              filename: 'init.yjs',
              contentType: 'application/octet-stream'
            });
            
            // Send request
            const response = await fetch(endpoint, {
              method: 'POST',
              headers: {
                ...headers,
                'Cookie': cookie,
                ...form.getHeaders()
              },
              body: form as any
            });
            
            const result = await response.json() as any;
            
            if (result.errors) {
              throw new Error(result.errors[0].message);
            }
            
            const workspace = result.data.createWorkspace;
            
            // Now create the actual document via WebSocket
            const wsUrl = endpoint.replace('https://', 'wss://').replace('http://', 'ws://').replace('/graphql', '');
            
            return new Promise((resolve) => {
              const socket = io(wsUrl, {
                transports: ['websocket'],
                path: '/socket.io/',
                extraHeaders: cookie ? { Cookie: cookie } : undefined
              });
              
              socket.on('connect', () => {
                // Join the workspace
                socket.emit('space:join', {
                  spaceType: 'workspace',
                  spaceId: workspace.id
                });
                
                // Send the document update
                setTimeout(() => {
                  const docUpdateBase64 = Buffer.from(docUpdate).toString('base64');
                  socket.emit('space:push-doc-update', {
                    spaceType: 'workspace',
                    spaceId: workspace.id,
                    docId: firstDocId,
                    update: docUpdateBase64
                  });
                  
                  // Wait longer for sync and disconnect
                  setTimeout(() => {
                    socket.disconnect();
                    resolve(text({
                      ...workspace,
                      name: name,
                      avatar: avatar,
                      firstDocId: firstDocId,
                      status: "success",
                      message: "Workspace created successfully",
                      url: `${process.env.AFFINE_BASE_URL}/workspace/${workspace.id}`
                    }));
                  }, 3000);
                }, 1000);
              });
              
              socket.on('error', () => {
                socket.disconnect();
                // Even if WebSocket fails, workspace was created
                resolve(text({
                  ...workspace,
                  name: name,
                  avatar: avatar,
                  firstDocId: firstDocId,
                  status: "partial",
                  message: "Workspace created (document sync may be pending)",
                  url: `${process.env.AFFINE_BASE_URL}/workspace/${workspace.id}`
                }));
              });
              
              // Timeout
              setTimeout(() => {
                socket.disconnect();
                resolve(text({
                  ...workspace,
                  name: name,
                  avatar: avatar,
                  firstDocId: firstDocId,
                  status: "success",
                  message: "Workspace created",
                  url: `${process.env.AFFINE_BASE_URL}/workspace/${workspace.id}`
                }));
              }, 10000);
            });
            
          } catch (error: any) {
            return text({ error: error.message, status: "failed" });
          }
        };
    
      server.registerTool(
        "create_workspace",
        {
          title: "Create Workspace",
          description: "Create a new workspace with initial document (accessible in UI)",
          inputSchema: {
            name: z.string().describe("Workspace name"),
            avatar: z.string().optional().describe("Avatar emoji or URL")
          }
        },
        createWorkspaceHandler as any
      );
      server.registerTool(
        "affine_create_workspace",
        {
          title: "Create Workspace",
          description: "Create a new workspace with initial document (accessible in UI)",
          inputSchema: {
            name: z.string().describe("Workspace name"),
            avatar: z.string().optional().describe("Avatar emoji or URL")
          }
        },
        createWorkspaceHandler as any
      );
      server.registerTool(
        "affine_create_workspace_fixed",
        {
          title: "Create Workspace (Fixed)",
          description: "Create a new workspace with initial document (backward compatible alias)",
          inputSchema: {
            name: z.string().describe("Workspace name"),
            avatar: z.string().optional().describe("Avatar emoji or URL")
          }
        },
        createWorkspaceHandler as any
      );
    
      // UPDATE WORKSPACE
      const updateWorkspaceHandler = async ({ id, public: isPublic, enableAi }: { id: string; public?: boolean; enableAi?: boolean }) => {
          try {
            const mutation = `
              mutation UpdateWorkspace($input: UpdateWorkspaceInput!) {
                updateWorkspace(input: $input) {
                  id
                  public
                }
              }
            `;
            
            const input: any = { id };
            if (isPublic !== undefined) input.public = isPublic;
            
            const data = await gql.request<{ updateWorkspace: any }>(mutation, { input });
            
            return text(data.updateWorkspace);
          } catch (error: any) {
            return text({ error: error.message });
          }
        };
      server.registerTool(
        "update_workspace",
        {
          title: "Update Workspace",
          description: "Update workspace settings",
          inputSchema: {
            id: z.string().describe("Workspace ID"),
            public: z.boolean().optional().describe("Make workspace public"),
            enableAi: z.boolean().optional().describe("Enable AI features")
          }
        },
        updateWorkspaceHandler as any
      );
      server.registerTool(
        "affine_update_workspace",
        {
          title: "Update Workspace",
          description: "Update workspace settings",
          inputSchema: {
            id: z.string().describe("Workspace ID"),
            public: z.boolean().optional().describe("Make workspace public"),
            enableAi: z.boolean().optional().describe("Enable AI features")
          }
        },
        updateWorkspaceHandler as any
      );
    
      // DELETE WORKSPACE
      const deleteWorkspaceHandler = async ({ id }: { id: string }) => {
          try {
            const mutation = `
              mutation DeleteWorkspace($id: String!) {
                deleteWorkspace(id: $id)
              }
            `;
            
            const data = await gql.request<{ deleteWorkspace: boolean }>(mutation, { id });
            
            return text({ success: data.deleteWorkspace, message: "Workspace deleted successfully" });
          } catch (error: any) {
            return text({ error: error.message });
          }
        };
      server.registerTool(
        "delete_workspace",
        {
          title: "Delete Workspace",
          description: "Delete a workspace permanently",
          inputSchema: {
            id: z.string().describe("Workspace ID")
          }
        },
        deleteWorkspaceHandler as any
      );
      server.registerTool(
        "affine_delete_workspace",
        {
          title: "Delete Workspace",
          description: "Delete a workspace permanently",
          inputSchema: {
            id: z.string().describe("Workspace ID")
          }
        },
        deleteWorkspaceHandler as any
      );
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states it's a list operation, implying read-only behavior, but doesn't cover critical aspects like pagination, sorting, authentication requirements, rate limits, or what 'available' means (e.g., accessible to the current user). For a tool with zero annotation coverage, this leaves significant gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without unnecessary words. It's front-loaded with the core action and resource, making it easy to parse. Every word earns its place.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's low complexity (0 parameters, no output schema) and lack of annotations, the description is minimally adequate but incomplete. It covers the basic purpose but misses behavioral details like output format or access constraints. For a simple list tool, it's passable but could benefit from more context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 0 parameters with 100% coverage, so no parameter documentation is needed. The description doesn't add parameter details, which is appropriate here. It implicitly confirms there are no required inputs, aligning with the schema.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb ('List') and resource ('all available AFFiNE workspaces'), making the purpose unambiguous. It doesn't explicitly differentiate from sibling tools like 'get_workspace' or 'create_workspace', but the scope is clear. The description avoids tautology by specifying the resource type.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention siblings like 'get_workspace' (for a single workspace) or 'create_workspace' (for creating new ones), nor does it specify prerequisites or contexts for usage. The agent must infer usage from the name alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

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/DAWNCR0W/affine-mcp-server'

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