@channel.io/be-user-chat-mcp
Version:
Backend UserChat Model Context Protocol server for Channel.io
98 lines • 3.69 kB
JavaScript
export var ContextSource;
(function (ContextSource) {
ContextSource["ENV_VARS"] = "env_vars";
ContextSource["HTTP_HEADERS"] = "http_headers";
})(ContextSource = ContextSource || (ContextSource = {}));
// ========================================
// Context 생성 유틸리티 함수들
// ========================================
/**
* 환경변수에서 RequestContext를 생성합니다 (stdio transport용)
*/
export function createContextFromEnv() {
const REDASH_URL = process.env.REDASH_URL;
const REDASH_API_KEY = process.env.REDASH_API_KEY;
const SENTRY_AUTH_TOKEN = process.env.SENTRY_AUTH_TOKEN;
const AWS_PROFILE = process.env.AWS_PROFILE;
if (!REDASH_URL) {
throw new Error("Missing required environment variable: REDASH_URL");
}
if (!REDASH_API_KEY) {
throw new Error("Missing required environment variable: REDASH_API_KEY");
}
if (!SENTRY_AUTH_TOKEN) {
throw new Error("Missing required environment variable: SENTRY_AUTH_TOKEN");
}
if (!AWS_PROFILE) {
throw new Error("Missing required environment variable: AWS_PROFILE");
}
return {
redashUrl: REDASH_URL,
redashApiKey: REDASH_API_KEY,
sentryAuthToken: SENTRY_AUTH_TOKEN,
awsProfile: AWS_PROFILE,
awsRegion: process.env.AWS_REGION,
awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
awsSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
awsSessionToken: process.env.AWS_SESSION_TOKEN,
};
}
/**
* HTTP 헤더에서 RequestContext를 생성합니다 (HTTP transport용)
*/
export function createContextFromHeaders(headers) {
// 헤더 검증
validateRequiredHeaders(headers);
return {
redashUrl: headers["x-redash-url"],
redashApiKey: headers["x-redash-api-key"],
sentryAuthToken: headers["x-sentry-auth-token"],
awsProfile: headers["x-aws-profile"],
awsRegion: headers["x-aws-region"],
awsAccessKeyId: headers["x-aws-access-key-id"],
awsSecretAccessKey: headers["x-aws-secret-access-key"],
awsSessionToken: headers["x-aws-session-token"],
};
}
/**
* 필수 헤더들이 모두 존재하는지 검증합니다
*/
export function validateRequiredHeaders(headers) {
// 기본 필수 헤더들
const BASIC_REQUIRED_HEADERS = [
"x-redash-url",
"x-redash-api-key",
"x-sentry-auth-token",
];
const missingBasic = BASIC_REQUIRED_HEADERS.filter((header) => !headers[header]);
if (missingBasic.length > 0) {
throw new Error(`Missing required headers: ${missingBasic.join(", ")}`);
}
// AWS 인증 방식 검증
const hasAwsProfile = !!headers["x-aws-profile"];
if (!hasAwsProfile) {
// Profile이 없으면 직접 credentials가 필요
const AWS_CREDENTIALS_HEADERS = [
"x-aws-region",
"x-aws-access-key-id",
"x-aws-secret-access-key",
"x-aws-session-token",
];
const missingCredentials = AWS_CREDENTIALS_HEADERS.filter((header) => !headers[header]);
if (missingCredentials.length > 0) {
throw new Error(`AWS Profile not provided. Missing required AWS credential headers: ${missingCredentials.join(", ")}`);
}
}
}
/**
* 환경변수가 모두 설정되어 있는지 검증합니다
*/
export function validateRequiredEnvVars() {
try {
createContextFromEnv(); // 환경변수 검증을 위해 한 번 호출해보기
}
catch (error) {
throw new Error(`Environment validation failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
//# sourceMappingURL=index.js.map