Skip to content

Commit 7247c10

Browse files
committed
Add MCP token type
1 parent 5bc8b0c commit 7247c10

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

packages/server/api/src/app/ai/mcp/openops-tools.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import fs from 'fs/promises';
1111
import { OpenAPI } from 'openapi-types';
1212
import os from 'os';
1313
import path from 'path';
14+
import { accessTokenManager } from '../../authentication/context/access-token-manager';
1415
import { MCPTool } from './types';
1516

1617
const INCLUDED_PATHS: Record<string, string[]> = {
@@ -68,7 +69,7 @@ async function getOpenApiSchemaPath(app: FastifyInstance): Promise<string> {
6869

6970
export async function getOpenOpsTools(
7071
app: FastifyInstance,
71-
authToken: string,
72+
userAuthToken: string,
7273
): Promise<MCPTool> {
7374
const basePath = system.getOrThrow<string>(
7475
AppSystemProp.OPENOPS_MCP_SERVER_PATH,
@@ -79,13 +80,15 @@ export async function getOpenOpsTools(
7980

8081
const tempSchemaPath = await getOpenApiSchemaPath(app);
8182

83+
const mcpToken = await accessTokenManager.generateMCPToken(userAuthToken);
84+
8285
const openopsClient = await experimental_createMCPClient({
8386
transport: new Experimental_StdioMCPTransport({
8487
command: pythonPath,
8588
args: [serverPath],
8689
env: {
8790
OPENAPI_SCHEMA_PATH: tempSchemaPath,
88-
AUTH_TOKEN: authToken,
91+
AUTH_TOKEN: mcpToken,
8992
API_BASE_URL: networkUtls.getInternalApiUrl(),
9093
OPENOPS_MCP_SERVER_PATH: basePath,
9194
LOGZIO_TOKEN: system.get<string>(SharedSystemProp.LOGZIO_TOKEN) ?? '',

packages/server/api/src/app/authentication/context/access-token-manager.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ export const accessTokenManager = {
3131
});
3232
},
3333

34+
async generateMCPToken(
35+
userToken: string,
36+
expiresInSeconds: number = openOpsRefreshTokenLifetimeSeconds,
37+
): Promise<string> {
38+
const principal = await this.extractPrincipal(userToken);
39+
40+
const secret = await jwtUtils.getJwtSecret();
41+
42+
return jwtUtils.sign({
43+
payload: {
44+
...principal,
45+
token: PrincipalType.MCP,
46+
},
47+
key: secret,
48+
expiresInSeconds,
49+
});
50+
},
51+
3452
async generateEngineToken({
3553
executionCorrelationId,
3654
projectId,

packages/shared/src/lib/authentication/model/principal-type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export enum PrincipalType {
2+
MCP = 'MCP',
23
USER = 'USER',
34
ENGINE = 'ENGINE',
45
SERVICE = 'SERVICE',

0 commit comments

Comments
 (0)