Introducing a new way to review your PRs.

Read the announcement

ever-co / ever-gauzy

Ever® Gauzy™ - Open Business Management Platform (ERP/CRM/HRM/ATS/PM) - https://gauzy.co

View on GitHub

27 documents · Updated Nov 23, 2026

docs / mcp-authentication-and-client

MCP authentication and client

The MCP client is an AI application such as Claude Desktop or ChatGPT that connects to Gauzy through the Model Context Protocol. Gauzy supports local clients through a managed MCP process and remote clients through HTTP or WebSocket endpoints. A separate MCP authentication application signs users in and issues OAuth tokens, while the MCP server protects the Gauzy resource and carries trusted user, tenant, and organization identity to Gauzy services.

Client boundary

Connection boundary

An AI client sends MCP requests to the MCP server. The server exposes Gauzy actions as MCP tools and sends the resulting calls to Gauzy services. The client does not become a Gauzy user by naming an organization or tenant. It must enter through a trusted credential path that creates server-side identity context.

The main connection paths are:

Client pathIdentity pathBest fit
StdioThe MCP process logs in to Gauzy with configured credentialsA local AI desktop app that starts a child process
HTTPThe client presents an OAuth bearer token and may use an MCP sessionWeb clients and hosted AI services
WebSocketThe client opens a persistent connection and may use an OAuth bearer token and MCP sessionLive applications

For HTTP and WebSocket, a caller on the MCP server's own machine can use the configured automatic-login account under the local connection checks. Other callers must use their own authenticated identity through the remote login path.

The following diagram shows where the identity boundary sits between an AI client and Gauzy services.

flowchart LR
    AI[AI client] -->|stdio| Local[MCP process]
    AI -->|HTTP or WebSocket with bearer token| MCP[MCP server]
    Local -->|Gauzy access token| Gauzy[Gauzy API]
    MCP -->|validated request context| Gauzy
    AI -->|OAuth login| Auth[MCP auth application]
    Auth -->|OAuth access token| AI
    MCP -.->|optional token introspection| Auth

The shared server surface is defined in packages/…/mcp-server.ts. The standalone apps/…/index.ts entry point and the desktop apps/…/electron-main.ts entry point host that surface. The auth application is a separate process and is not embedded in the MCP server.

References: packages/…/mcp-server.ts• L227-340 apps/…/index.ts• L36-62 apps/…/electron-main.ts• L91-143 packages/…/http-transport.ts• L80-93 apps/…/README.md• L216-222

Startup paths

Standalone client process

The standalone application is the normal path for an AI client that starts MCP as a child process. It creates the shared server, initializes session support, selects a transport, and connects the server before it accepts MCP requests. Stdio is the default path, so protocol data can pass between the AI client and the process without a network port.

The same entry point has a test path. The --test option builds the server and reports whether the MCP surface can start, then exits instead of serving requests. HTTP and WebSocket startup use the same shared server but expose a network address selected by transport settings.

sequenceDiagram
    participant Client as AI client
    participant App as apps/mcp
    participant Shared as shared MCP server
    participant Gauzy as Gauzy API

    Client->>App: Start MCP process
    App->>Shared: Initialize sessions and tools
    Shared->>Shared: Select stdio, HTTP, or WebSocket transport
    Shared-->>Client: MCP connection is ready
    Client->>Shared: initialize / tools/list / tools/call
    Shared->>Gauzy: Authenticated Gauzy request
    Gauzy-->>Shared: Gauzy result
    Shared-->>Client: MCP result

References: apps/…/index.ts• L36-61 apps/…/README.md• L49-91 packages/…/mcp-server.ts• L287-340 packages/…/transport-factory.ts• L17-41

Desktop host

The desktop application gives a user a local control surface for the same MCP server. Electron starts the server in the main process before it creates the status window. The window can ask for status and request a restart through a narrow preload bridge. It does not access the server or Node.js APIs directly.

This path is useful when the user wants a managed local process rather than a command-line child process. The desktop host passes the configured Gauzy API settings to the shared server and stops that server when the application quits.

References: apps/…/electron-main.ts• L107-143 apps/…/electron-main.ts• L181-185 apps/…/preload.ts• L58-100 apps/…/index.html• L503-595

Authentication service startup

The MCP authentication application starts as an independent Nest application. Its root module loads shared configuration, password-hash support, a Gauzy database connection, and the OAuth module that links OAuth identity to Gauzy users. Its default port is 3003, and MCP_AUTH_SESSION_SECRET is required before the OAuth service can be created.

On module start, it registers known public MCP clients. It also registers the MCP server as a resource server when the configured introspection client ID and secret are present. The MCP server uses those matching credentials to ask the auth service whether a bearer token is active. Missing or rejected resource-server credentials are logged and do not stop the auth application, but introspection is then unavailable.

The auth application publishes authorization, token, user-info, key-set, registration, introspection, and discovery endpoints. It issues OAuth tokens and owns the login boundary. The MCP server owns resource protection and MCP request handling.

References: apps/…/main.ts• L14-80 apps/…/app.module.ts• L12-30 apps/…/mcp-oauth.module.ts• L7-15 apps/…/mcp-oauth.service.ts• L27-94 apps/…/mcp-oauth.service.ts• L129-153 apps/…/mcp-oauth.service.resource-server.spec.ts• L15-62 apps/…/package.json• L20-26

Credential flows

Gauzy API login

The local client flow uses Gauzy credentials to obtain a Gauzy access token and refresh token. The client then asks Gauzy for the current user and captures tenant and organization context from that response. Later API calls receive the access token automatically, and an expired access token can be refreshed before a request is retried.

This flow is used by the standalone and desktop-hosted server when configured for automatic login. The authentication tools also expose explicit login, logout, status, and refresh actions through MCP. Credentials belong to the local server configuration in this mode. They are not an OAuth client secret for the remote MCP flow.

References: packages/…/auth-manager.ts• L119-223 packages/…/auth-manager.ts• L225-280 packages/…/api-client.ts• L29-60 packages/…/auth.ts• L19-106

MCP sign-in controls

The MCP auth application checks the Gauzy user and employee records before it creates an OAuth identity. An employee is a user with an employee record. A user with no employee record can sign in, and a user with several employee records can sign in when at least one record is active and not archived. It also refuses password-only sign-in for an account with two-factor authentication enabled because this form cannot collect the second factor.

Password failures use the same per-account failure counter as Gauzy API login. The counter can block the account for a period, even when failed guesses come from different network addresses. Database or password-service errors release the attempt instead of counting it as a bad password.

References: apps/…/oauth-user.service.ts• L34-73 apps/…/oauth-user.service.ts• L111-185 apps/…/user.module.ts• L11-23 apps/…/oauth-user.service.spec.ts• L27-151 apps/…/oauth-user.service.deactivated-employee.spec.ts• L58-109

OAuth client flow

The remote client flow places the MCP auth application between the AI client and the MCP server. The AI client discovers the authorization service, completes the OAuth flow, and sends the resulting bearer token to the MCP server. The token identifies the Gauzy user and the client that requested access.

The MCP server publishes protected-resource metadata so a client can learn which authorization server protects the resource and which scopes it needs. When authorization is enabled, the server checks the bearer-token boundary before it routes MCP requests. It can validate token claims directly or ask the registered MCP resource server at mcp-auth to introspect the token. Missing credentials produce an unauthorized response. A request with too few scopes is forbidden.

The separation matters: the auth application owns login and token issue, while the MCP server owns resource protection and MCP request handling. Keep the issuer, audience, resource URI, and introspection credentials aligned when changing endpoints or deployment settings.

References: packages/…/auth-types.ts• L14-33 packages/…/authorization-middleware.ts• L32-109 packages/…/authorization-middleware.ts• L112-160 packages/…/http-transport.ts• L320-401 apps/…/mcp-oauth.service.ts• L135-153 apps/…/README.md• L182-187 apps/…/README.md• L260-267

Identity context

Gauzy identity payload

The auth application accepts only an active, non-archived Gauzy user whose password matches the stored password hash. It also refuses a user whose employee records are all inactive or archived, or whose account has two-factor authentication enabled for this password-only form. It maps the accepted user into the OAuth identity payload with a user ID, email, tenant ID, an active organization when available, role, email-verification state, and profile image.

The organization value comes from an active and non-archived organization relation. If no such relation exists, the service falls back to the user's default organization. This makes the organization value part of the issued identity context rather than a value supplied by an AI client at tool-call time.

The user lookup distinguishes an account that no longer exists from a lookup that could not run. A missing, inactive, or archived account, or a user with no active employee record, invalidates the refresh grant. A database or timeout error stays an error so the client can retry instead of discarding a still-valid refresh token. The OAuth service can revoke tokens for users who later fail these account checks.

References: apps/…/oauth-user.service.ts• L34-73 apps/…/oauth-user.service.ts• L75-109 apps/…/oauth-user.service.ts• L161-210 apps/…/mcp-oauth.service.ts• L104-126 apps/…/oauth-user.service.deactivated-employee.spec.ts• L111-163

Request context

After a session is accepted, the MCP server carries the user ID, tenant ID, organization ID, session ID, and connection ID with the request. For OAuth requests, it also carries the token subject, client ID, scopes, and authorization result. This context is the handoff from authentication to Gauzy tool work.

Sessions can serve more than one HTTP request or WebSocket connection. The session manager checks that a session remains active and that its user, organization, and tenant still match the current authentication state. It also rejects access to a different organization or tenant when a tool operation asks for that boundary to be checked.

Downstream tool work should use this server-created context as its identity source. Treat organization and tenant values supplied directly by an AI client as request data, not as proof of access.

References: packages/…/session-manager.ts• L20-42 packages/…/session-manager.ts• L152-205 packages/…/session-manager.ts• L347-385 packages/…/http-transport.ts• L697-723 packages/…/websocket-transport.ts• L419-464

Session lifecycle

An MCP session groups a user identity with one or more connections. HTTP requests can use a session header, query value, or cookie. WebSocket clients can provide a session identifier during connection setup. The server refreshes authentication when possible, tracks activity, and removes expired sessions and closed connections.

Session protection and OAuth protection solve different problems. A session links repeated connections to one user context. An OAuth token proves that a remote client is allowed to reach the protected MCP resource. A deployment can use both, with the bearer token protecting the request and the session carrying connection state.

References: packages/…/session-middleware.ts• L58-160 packages/…/session-middleware.ts• L168-239 packages/…/session-store.ts• L107-185 packages/…/session-store.ts• L228-273

Client and server contracts

MCP protocol contract

The client and server communicate with MCP messages over the selected transport. The shared server advertises tool capability and handles the core initialize, tools/list, and tools/call operations. Tool input is checked before the call reaches Gauzy-facing work.

Stdio is process-based and has no server URL. HTTP and WebSocket have network addresses and can use bearer authorization, sessions, rate limits, and origin controls. The transport choice changes how a client connects, not the set of Gauzy capabilities exposed by the shared server.

References: packages/…/protocol.ts• L1 packages/…/mcp-server.ts• L231-283 packages/…/http-transport.ts• L377-401 packages/…/websocket-transport.ts• L477-543

Configuration contract

The local client path needs a Gauzy API base URL. It needs an email and password when automatic login is enabled. The remote OAuth path needs a public resource URI, an authorization-server issuer, matching token validation settings, and the required scopes. If the MCP server uses token introspection, mcp-auth and the MCP server must share the introspection client ID and secret.

Production HTTP and WebSocket deployments also need explicit allowed origins and trusted proxy settings. Use the same identity values across the auth service, MCP server, and AI client configuration. A mismatch between the token issuer, audience, resource URI, introspection endpoint, or endpoint URL can look like a client connection failure even when both processes are healthy.

References: packages/…/environment.ts• L31-87 packages/…/config.ts• L54-140 packages/…/auth-types.ts• L70-89 apps/…/mcp-oauth.service.ts• L27-78 apps/…/README.md• L168-207

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the MCP auth issuer, JWKS URL, audience, protected-resource URI, required scopes, and introspection client credentials aligned across the auth service and each network transport.
  • If the identity payload or account eligibility checks change, check refresh-time user lookup, token revocation, session context, and every downstream path that reads user, tenant, or organization identity.
  • If a transport changes its session header, cookie, or connection query value, update both the client contract and the matching session middleware.
  • If the startup order changes, keep the server ready before the standalone client or desktop UI reports a usable connection.
  • If OAuth is enabled for a deployment, verify that token validation or introspection is active before exposing the network endpoint.
  • If MCP password login changes, keep its account failure counter, deactivated-employee checks, and password-only two-factor rule consistent with the available Gauzy login controls.

Important failure modes

TriggerConsequenceGuard
MCP_AUTH_SESSION_SECRET is missingThe auth application fails during service construction and cannot startProvide a secure session secret before startup
MCP introspection client ID or secret is missing or rejectedThe auth app starts, but the MCP server cannot use mcp-auth to check bearer-token statusSet the same valid resource-server credentials on both services
The auth token has the wrong issuer, audience, or scopeThe MCP server returns 401 or 403 and the AI client cannot call toolsKeep resource metadata and token claims aligned
The Gauzy API URL or automatic-login credentials are invalidLocal stdio or desktop-hosted tool calls cannot obtain a Gauzy tokenTest API reachability and login before connecting the AI client
Password guesses reach the per-account failure thresholdEven a later correct password is blocked until the lockout endsUse the shared login-attempt policy and honor Retry-After
An account has two-factor authentication enabledPassword-only MCP sign-in is refused because the form cannot collect the second factorUse a login path that supports the account's second factor
A user is inactive, archived, or has no active employee recordSign-in and refresh-time user checks reject the account, and its tokens can be revokedKeep user and employee status checks active at every authentication boundary
A refresh-token lookup fails because the database is unavailableThe refresh request fails without invalidating a still-valid refresh tokenRestore database access and retry the grant
A session user differs from the active user, tenant, or organizationSession validation or authorization denies the requestCreate sessions from trusted authentication context and do not reuse them across identities
Production origin or proxy settings are missing or too broadBrowser or WebSocket clients are rejected, or client IP checks can be unreliableSet explicit allowed origins and trusted proxies for the deployment

Key files

FileWhy to read it
apps/…/index.tsShows the standalone MCP startup and test paths.
apps/…/README.mdShows supported client paths, local identity rules, and the remote OAuth and introspection contract.
apps/…/main.tsShows how the dedicated OAuth application is mounted and started.
apps/…/app.module.tsShows the auth application's shared configuration, database, password, and OAuth service boundary.
apps/…/mcp-oauth.service.tsShows OAuth setup, identity mapping, token checks, and MCP resource-server registration.
apps/…/oauth-user.service.tsShows which Gauzy users and employees can authenticate to MCP, how password failures and two-factor accounts are controlled, and how refresh-time checks handle account changes.
apps/…/user.module.tsShows the auth user boundary, account-failure service, and two-factor record used by password-only sign-in.
apps/…/package.jsonShows the separate auth application's build, serve, and test lifecycle.
packages/…/auth-manager.tsShows the local Gauzy login, refresh, and tenant or organization capture flow.
packages/…/authorization-middleware.tsShows the network resource boundary for bearer tokens and scopes.
packages/…/session-manager.tsShows how authenticated identity becomes session and request context.
packages/…/mcp-server.tsShows the shared MCP server used by standalone and desktop hosts.
packages/…/transport-factory.tsShows how the server selects the client connection mode.
apps/…/electron-main.tsShows the desktop startup order and lifecycle control.