Skip to main content
Glama

Create Workspace

create_workspace

Generate a new workspace with an initial document for collaborative content creation in AFFiNE, using the MCP server API. Define workspace name and optional avatar for easy organization.

Instructions

Create a new workspace with initial document (accessible in UI)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
avatarNoAvatar emoji or URL
nameYesWorkspace name

Implementation Reference

  • Main handler function executing the create_workspace tool: generates initial Yjs workspace and document, sends GraphQL mutation via multipart form, and syncs document via WebSocket.
    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" });
        }
      };
  • Registration of the 'create_workspace' MCP tool, including title, description, Zod input schema, and reference to the handler function.
    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
    );
  • Zod input schema defining parameters for the create_workspace tool: required 'name' string and optional 'avatar' string.
      name: z.string().describe("Workspace name"),
      avatar: z.string().optional().describe("Avatar emoji or URL")
    }
  • Helper function to generate initial Yjs updates for new workspace root and first document with AFFiNE block structure.
    function createInitialWorkspaceData(workspaceName: string = 'New Workspace') {
      // Create workspace root YDoc
      const rootDoc = new Y.Doc();
      
      // Set workspace metadata
      const meta = rootDoc.getMap('meta');
      meta.set('name', workspaceName);
      meta.set('avatar', '');
      
      // Create pages array with initial document
      const pages = new Y.Array();
      const firstDocId = generateDocId();
      
      // Add first document metadata
      const pageMetadata = new Y.Map();
      pageMetadata.set('id', firstDocId);
      pageMetadata.set('title', 'Welcome to ' + workspaceName);
      pageMetadata.set('createDate', Date.now());
      pageMetadata.set('tags', new Y.Array());
      
      pages.push([pageMetadata]);
      meta.set('pages', pages);
      
      // Create settings
      const setting = rootDoc.getMap('setting');
      setting.set('collections', new Y.Array());
      
      // Encode workspace update
      const workspaceUpdate = Y.encodeStateAsUpdate(rootDoc);
      
      // Create the actual document
      const docYDoc = new Y.Doc();
      const blocks = docYDoc.getMap('blocks');
      
      // Create page block with proper structure
      const pageId = generateDocId();
      const pageBlock = new Y.Map();
      pageBlock.set('sys:id', pageId);
      pageBlock.set('sys:flavour', 'affine:page');
      
      // Title as Y.Text
      const titleText = new Y.Text();
      titleText.insert(0, 'Welcome to ' + workspaceName);
      pageBlock.set('prop:title', titleText);
      
      // Children
      const pageChildren = new Y.Array();
      pageBlock.set('sys:children', pageChildren);
      
      blocks.set(pageId, pageBlock);
      
      // Add surface block (required)
      const surfaceId = generateDocId();
      const surfaceBlock = new Y.Map();
      surfaceBlock.set('sys:id', surfaceId);
      surfaceBlock.set('sys:flavour', 'affine:surface');
      surfaceBlock.set('sys:parent', pageId);
      surfaceBlock.set('sys:children', new Y.Array());
      
      blocks.set(surfaceId, surfaceBlock);
      pageChildren.push([surfaceId]);
      
      // Add note block with xywh
      const noteId = generateDocId();
      const noteBlock = new Y.Map();
      noteBlock.set('sys:id', noteId);
      noteBlock.set('sys:flavour', 'affine:note');
      noteBlock.set('sys:parent', pageId);
      noteBlock.set('prop:displayMode', 'DocAndEdgeless');
      noteBlock.set('prop:xywh', '[0,0,800,600]');
      noteBlock.set('prop:index', 'a0');
      noteBlock.set('prop:lockedBySelf', false);
      
      const noteChildren = new Y.Array();
      noteBlock.set('sys:children', noteChildren);
      
      blocks.set(noteId, noteBlock);
      pageChildren.push([noteId]);
      
      // Add initial paragraph
      const paragraphId = generateDocId();
      const paragraphBlock = new Y.Map();
      paragraphBlock.set('sys:id', paragraphId);
      paragraphBlock.set('sys:flavour', 'affine:paragraph');
      paragraphBlock.set('sys:parent', noteId);
      paragraphBlock.set('sys:children', new Y.Array());
      paragraphBlock.set('prop:type', 'text');
      
      const paragraphText = new Y.Text();
      paragraphText.insert(0, 'This workspace was created by AFFiNE MCP Server');
      paragraphBlock.set('prop:text', paragraphText);
      
      blocks.set(paragraphId, paragraphBlock);
      noteChildren.push([paragraphId]);
      
      // Set document metadata
      const docMeta = docYDoc.getMap('meta');
      docMeta.set('id', firstDocId);
      docMeta.set('title', 'Welcome to ' + workspaceName);
      docMeta.set('createDate', Date.now());
      docMeta.set('tags', new Y.Array());
      docMeta.set('version', 1);
      
      // Encode document update
      const docUpdate = Y.encodeStateAsUpdate(docYDoc);
      
      return {
        workspaceUpdate,
        firstDocId,
        docUpdate
      };
    }
  • Utility function to generate AFFiNE-style 10-char document IDs used in workspace creation.
    function generateDocId(): string {
      const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
      let id = '';
      for (let i = 0; i < 10; i++) {
        id += chars.charAt(Math.floor(Math.random() * chars.length));
      }
      return id;
    }
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 mentions that a workspace is created with an 'initial document (accessible in UI)', which hints at a side effect, but doesn't cover critical aspects like whether this requires authentication, what happens on failure, if the document has default content, or the response format. For a mutation tool with zero annotation coverage, this is a significant gap in transparency.

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

Conciseness4/5

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

The description is a single, efficient sentence that front-loads the core action. It avoids unnecessary words and gets straight to the point. However, it could be slightly more structured by explicitly mentioning parameters or outcomes, but it earns its place by being clear and to the point.

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

Completeness2/5

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

Given the complexity of creating a workspace (a mutation operation), the lack of annotations, and no output schema, the description is incomplete. It doesn't explain what the tool returns, error conditions, or behavioral details like authentication needs. For a tool with two parameters and significant implications, more context is needed to guide the agent effectively.

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

Parameters3/5

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

The input schema has 100% description coverage, with clear documentation for 'name' and 'avatar' parameters. The description adds no additional semantic context beyond what the schema provides, such as examples or constraints. Since the schema does the heavy lifting, the baseline score of 3 is appropriate, as the description doesn't compensate but also doesn't detract.

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 'Create' and resource 'new workspace', specifying it includes an 'initial document (accessible in UI)'. This distinguishes it from sibling tools like 'update_workspace' or 'list_workspaces', though it doesn't explicitly differentiate from all siblings. The purpose is specific but could be more precise about what makes this creation unique.

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 prerequisites, such as authentication or permissions, or when not to use it (e.g., for updating existing workspaces). With siblings like 'update_workspace' and 'list_workspaces', this lack of context leaves the agent guessing about appropriate usage scenarios.

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