Skip to content

Add organizationContext for multi-tenant applications - #6

Merged
zainhoda merged 5 commits into
mainfrom
copilot/add-organization-context-support
Jan 31, 2026
Merged

zainhoda merged 5 commits into
mainfrom
copilot/add-organization-context-support

Conversation

Copilot AI commented Jan 31, 2026 •

Copy link
Copy Markdown
Contributor

Multi-tenant applications need organization-scoped functions where auth extracts organization identity and injects it into resolvers, parallel to the existing userContext() pattern.

Implementation

Core API

  • Added organizationContext() helper with symbol-based metadata tracking
  • Extended AuthResult type with optional organization field
  • Organization context fields stripped from public API/MCP schemas, injected at runtime
  • Startup validation ensures auth returns organization when functions require it

Runtime Integration

  • Lockfile tracks usesOrganizationContext in function shapes for change detection
  • MCP tools executor injects organization from auth result
  • API router merges organization into function arguments before validation

Browser UI

  • "Org Context" filter button with count badge (styled with magenta/building icon)
  • Function detail panels show organization context badge
  • Mock organization context available in test mode

Documentation

  • New /docs/src/content/docs/api/organization-context.md with multi-tenant patterns
  • Claude skills template updated with organization examples
  • Auth patterns for query params, headers, subdomains

Usage

import { defineOntology, organizationContext, z } from 'ont-run';

export default defineOntology({
  auth: async (req) => {
    const orgId = new URL(req.url).searchParams.get('org_id');
    const org = await db.organizations.findById(orgId);
    await verifyMembership(user.id, org.id);
    
    return {
      groups: ['member'],
      organization: { id: org.id, name: org.name },
    };
  },
  
  functions: {
    createProject: {
      description: 'Create a project in the organization',
      access: ['member'],
      entities: ['Project'],
      inputs: z.object({
        name: z.string(),
        // Organization context injected from auth, hidden from callers
        currentOrg: organizationContext(z.object({
          id: z.string(),
          name: z.string(),
        })),
      }),
      resolver: async (ctx, args) => {
        return db.projects.create({
          name: args.name,
          organizationId: args.currentOrg.id, // Auto-injected
        });
      },
    },
  },
});

Can be used alone or combined with userContext() for functions requiring both identities.

Review UI Validation

Overview with both context filters:
Review UI Overview

Filter buttons highlighted:
Organization Context Filter

UI correctly displays:

  • "Org Context (3)" button showing 3 functions using organization context
  • "User Context (2)" button showing 2 functions using user context
  • Distinct styling (cyan/building icon vs teal/user icon)
  • Proper tracking in lockfile changes

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits January 31, 2026 12:50
Co-authored-by: zainhoda <7146154+zainhoda@users.noreply.github.com>
Co-authored-by: zainhoda <7146154+zainhoda@users.noreply.github.com>
Co-authored-by: zainhoda <7146154+zainhoda@users.noreply.github.com>
Co-authored-by: zainhoda <7146154+zainhoda@users.noreply.github.com>
Copilot AI changed the title [WIP] Add organization context support for multi-tenant applications Add organization context for multi-tenant applications Jan 31, 2026
Copilot AI requested a review from zainhoda January 31, 2026 12:59
Copilot AI changed the title Add organization context for multi-tenant applications Add organizationContext for multi-tenant applications Jan 31, 2026
@zainhoda
zainhoda marked this pull request as ready for review January 31, 2026 13:36
@zainhoda
zainhoda merged commit 215de54 into main Jan 31, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants