-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcontext.ts
More file actions
211 lines (171 loc) · 5.57 KB
/
context.ts
File metadata and controls
211 lines (171 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { tryCatch } from "@trigger.dev/core/utils";
import { ApiClient } from "@trigger.dev/core/v3";
import path from "node:path";
import { CliApiClient } from "../apiClient.js";
import { loadConfig } from "../config.js";
import { mcpAuth } from "./auth.js";
import {
hasElicitationCapability,
hasRootsCapability,
hasSamplingCapability,
} from "./capabilities.js";
import { FileLogger } from "./logger.js";
import { fileURLToPath } from "node:url";
export type McpContextOptions = {
projectRef?: string;
fileLogger?: FileLogger;
apiUrl?: string;
profile?: string;
devOnly?: boolean;
envOnly?: string[];
disableDeployment?: boolean;
readonly?: boolean;
};
export class McpContext {
public readonly server: McpServer;
public readonly options: McpContextOptions;
constructor(server: McpServer, options: McpContextOptions) {
this.server = server;
this.options = options;
}
get logger() {
return this.options.fileLogger;
}
public async getAuth() {
const auth = await mcpAuth({
server: this.server,
defaultApiUrl: this.options.apiUrl,
profile: this.options.profile,
context: this,
});
if (!auth.ok) {
throw new Error(auth.error);
}
return auth;
}
public async getCliApiClient(branch?: string) {
const auth = await this.getAuth();
return new CliApiClient(auth.auth.apiUrl, auth.auth.accessToken, branch);
}
public async getApiClient(options: {
projectRef: string;
environment: string;
scopes: string[];
branch?: string;
}) {
const cliApiClient = await this.getCliApiClient(options.branch);
const jwt = await cliApiClient.getJWT(options.projectRef, options.environment, {
claims: {
scopes: options.scopes,
},
});
if (!jwt.success) {
throw new Error(
`Could not get the authentication token for the project ${options.projectRef} in the ${options.environment} environment. Please try again.`
);
}
return new ApiClient(cliApiClient.apiURL, jwt.data.token);
}
public async getCwd() {
if (!this.hasRootsCapability) {
return undefined;
}
const response = await this.server.server.listRoots();
if (response.roots.length >= 1) {
return response.roots[0]?.uri ? fileURLToPath(response.roots[0].uri) : undefined;
}
return undefined;
}
public async getProjectRef(options: { projectRef?: string; cwd?: string }) {
if (options.projectRef) {
return options.projectRef;
}
const projectDir = await this.getProjectDir({ cwd: options.cwd });
if (!projectDir.ok) {
throw new Error(projectDir.error);
}
const [_, config] = await tryCatch(loadConfig({ cwd: projectDir.cwd }));
if (
config?.configFile &&
typeof config.project === "string" &&
config.project.startsWith("proj_")
) {
return config.project;
}
throw new Error("No project ref found. Please provide a projectRef.");
}
public async getProjectDir({ cwd }: { cwd?: string }) {
// If cwd is a path to the actual trigger.config.ts file, then we should set the cwd to the directory of the file
let $cwd = cwd ? (path.extname(cwd) !== "" ? path.dirname(cwd) : cwd) : undefined;
function isRelativePath(filePath: string) {
return !path.isAbsolute(filePath);
}
if (!cwd) {
if (!this.hasRootsCapability) {
return {
ok: false,
error:
"The current MCP server does not support the roots capability, so please call the tool again with a projectRef or an absolute path as cwd parameter",
};
}
$cwd = await this.getCwd();
} else if (isRelativePath(cwd)) {
if (!this.hasRootsCapability) {
return {
ok: false,
error:
"The current MCP server does not support the roots capability, so please call the tool again with a projectRef or an absolute path as cwd parameter",
};
}
const resolvedCwd = await this.getCwd();
if (!resolvedCwd) {
return {
ok: false,
error: "No current working directory found. Please provide a projectRef or a cwd.",
};
}
$cwd = path.resolve(resolvedCwd, cwd);
}
if (!$cwd) {
return {
ok: false,
error: "No current working directory found. Please provide a projectRef or a cwd.",
};
}
return {
ok: true,
cwd: $cwd,
};
}
public async getDashboardUrl(path: string) {
const auth = await this.getAuth();
return `${auth.dashboardUrl}${path}`;
}
public get hasRootsCapability() {
return hasRootsCapability(this.server);
}
public get hasSamplingCapability() {
return hasSamplingCapability(this.server);
}
public get hasElicitationCapability() {
return hasElicitationCapability(this.server);
}
public isEnvironmentAllowed(environment: string): boolean {
// Normalize the environment name for comparison
const normalizedEnv = environment.trim().toLowerCase();
// If envOnly is specified, use that (devOnly is already converted to envOnly)
if (this.options.envOnly && this.options.envOnly.length > 0) {
// Note: envOnly is already normalized to lowercase in mcp.ts
return this.options.envOnly.includes(normalizedEnv);
}
// If no restrictions, all environments are allowed
return true;
}
public getAllowedEnvironments(): string {
if (this.options.envOnly && this.options.envOnly.length > 0) {
return this.options.envOnly.join(", ");
}
return "all environments";
}
}