cambios mcp remoto

This commit is contained in:
Jordan Diaz
2026-04-17 20:03:02 +00:00
parent d41a94b57d
commit 2ac01acd61
15 changed files with 344 additions and 123 deletions

View File

@@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import { resolveCurrentProjectDir } from '../files/helpers.js';
/**
* Check if the current user has write access to a table.
@@ -16,7 +17,7 @@ import path from 'path';
* en lugar del .acai para determinar el modo.
*/
export function canAccessTable(tableName) {
const projectDir = process.env.ACAI_PROJECT_DIR || "";
const projectDir = resolveCurrentProjectDir();
if (!projectDir) return { allowed: true }; // no project dir, don't block
const acaiFile = path.join(projectDir, ".acai");

View File

@@ -1,21 +1,45 @@
import axios from "axios";
import { resolveCurrentAcaiUser } from "./sessionHelpers.js";
const PYTHON_BASE = `http://app:${process.env.ACAI_PORT || 9091}`;
export async function pythonPost(path, data, timeout = 120000) {
/**
* Construye el set de headers comunes para llamadas al server Python interno.
* Inyecta automaticamente `X-Acai-User` cuando hay sesion MCP activa con
* `acai_user` conocido, lo que permite a los endpoints autenticados identificar
* al usuario sin Authorization Basic.
*/
function buildPythonHeaders(extra = {}) {
const authHeader = process.env.ACAI_AUTH_HEADER || "";
const mode = process.env.ACAI_MODE_OVERRIDE || process.env.ACAI_MODE || "";
const role = process.env.ACAI_ROLE_OVERRIDE || "";
const acaiUser = resolveCurrentAcaiUser();
return {
"Content-Type": "application/json",
...(authHeader ? { "Authorization": authHeader } : {}),
...(mode ? { "X-Acai-Mode": mode } : {}),
...(role ? { "X-Acai-Role": role } : {}),
...(acaiUser ? { "X-Acai-User": acaiUser } : {}),
...extra,
};
}
export async function pythonPost(path, data, timeout = 120000) {
const response = await axios.post(`${PYTHON_BASE}${path}`, data, {
headers: {
"Content-Type": "application/json",
...(authHeader ? { "Authorization": authHeader } : {}),
...(mode ? { "X-Acai-Mode": mode } : {}),
...(role ? { "X-Acai-Role": role } : {}),
},
headers: buildPythonHeaders(),
timeout,
maxBodyLength: Infinity,
maxContentLength: Infinity,
});
return response.data;
}
export async function pythonGet(path, params = null, timeout = 30000) {
const response = await axios.get(`${PYTHON_BASE}${path}`, {
params: params || undefined,
headers: buildPythonHeaders(),
timeout,
});
return response.data;
}

View File

@@ -0,0 +1,27 @@
/**
* Helpers reutilizables para resolver datos derivados de la sesion MCP en curso.
*
* Usan AsyncLocalStorage (`utils/sessionContext.js`) para recuperar el
* `mcpSessionId` activo y leer informacion asociada desde
* `auth/credentials.js`. En modo stdio (sin HTTP) devuelven `null` y el caller
* decide como actuar.
*/
import { getCurrentSessionId } from "../../utils/sessionContext.js";
import { getMcpSessionCredentials } from "../../auth/credentials.js";
/**
* Recupera el `acai_user` de la sesion HTTP activa (si existe).
*
* Se usa para inyectar el header `X-Acai-User` en llamadas al server Python,
* evitando asi depender de Authorization Basic y permitiendo que los endpoints
* autenticados (p.ej. `/api/generate-image`, `/api/files/write`) identifiquen
* al usuario propietario del proyecto.
*
* @returns {string|null}
*/
export function resolveCurrentAcaiUser() {
const sessionId = getCurrentSessionId();
if (!sessionId) return null;
const creds = getMcpSessionCredentials(sessionId);
return creds?.acai_user || null;
}