Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/agents/sample.config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[API_KEYS]
OPENAI = ""
GROQ = ""
ANTHROPIC = ""
DEEPSEEK = ""
GEMINI = ""

[API_ENDPOINTS]
OLLAMA = ""

[VECTOR_DB]
POSTGRES_USER="cairocoder"
POSTGRES_PASSWORD="cairocoder"
POSTGRES_ROOT_DB="cairocoder"
POSTGRES_HOST="localhost"
POSTGRES_PORT="5432"

[GENERAL]
PORT = 3_001
SIMILARITY_MEASURE = "cosine"

[HOSTED_MODE]
DEFAULT_CHAT_PROVIDER = "gemini"
DEFAULT_CHAT_MODEL = "Gemini Flash 2.5"
DEFAULT_FAST_CHAT_PROVIDER = "gemini"
DEFAULT_FAST_CHAT_MODEL = "Gemini Flash 2.5"
DEFAULT_EMBEDDING_PROVIDER = "openai"
DEFAULT_EMBEDDING_MODEL = "Text embedding 3 large"
6 changes: 5 additions & 1 deletion packages/agents/src/core/agentFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Embeddings } from '@langchain/core/embeddings';
import { getAgentConfig } from '../config/agent';
import EventEmitter from 'events';
import { RagPipeline } from './pipeline/ragPipeline';
import { McpPipeline } from './pipeline/mcpPipeline';
import { VectorStore } from '../db/postgresVectorStore';
import { LLMConfig } from '../types';

Expand All @@ -13,9 +14,12 @@ export class RagAgentFactory {
llm: LLMConfig,
embeddings: Embeddings,
vectorStore: VectorStore,
mcpMode: boolean = false,
): EventEmitter {
const config = getAgentConfig(vectorStore);
const pipeline = new RagPipeline(llm, embeddings, config);
const pipeline = mcpMode
? new McpPipeline(llm, embeddings, config)
: new RagPipeline(llm, embeddings, config);
return pipeline.execute({
query: message,
chatHistory: history,
Expand Down
62 changes: 62 additions & 0 deletions packages/agents/src/core/pipeline/mcpPipeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { RagPipeline } from './ragPipeline';
import { RagInput, StreamHandler } from '../../types';
import { logger, TokenTracker } from '../../utils';

/**
* MCP Pipeline that extends RAG pipeline but stops at document retrieval
* without generating answers, returning raw documents instead.
*/
export class McpPipeline extends RagPipeline {
protected async runPipeline(
input: RagInput,
handler: StreamHandler,
): Promise<void> {
try {
// Reset token counters at the start of each pipeline run
TokenTracker.resetSessionCounters();

logger.info('Starting MCP pipeline', { query: input.query });

// Step 1: Process the query
const processedQuery = await this.queryProcessor.process(input);
logger.debug('Processed query:', processedQuery);

// Step 2: Retrieve documents
const retrieved = await this.documentRetriever.retrieve(
processedQuery,
input.sources,
);
handler.emitSources(retrieved.documents);

// Step 3: Return raw documents without answer generation
logger.info('MCP mode - returning raw documents');

const rawDocuments = retrieved.documents.map(doc => ({
pageContent: doc.pageContent,
metadata: doc.metadata
}));

handler.emitResponse({
content: JSON.stringify(rawDocuments, null, 2),
} as any);

logger.debug('MCP pipeline ended');

// Log final token usage
const tokenUsage = TokenTracker.getSessionTokenUsage();
logger.info('MCP Pipeline completed', {
query: input.query,
tokenUsage: {
promptTokens: tokenUsage.promptTokens,
responseTokens: tokenUsage.responseTokens,
totalTokens: tokenUsage.totalTokens
}
});

handler.emitEnd();
} catch (error) {
logger.error('MCP Pipeline error:', error);
handler.emitError('An error occurred while processing your request');
}
}
}
8 changes: 4 additions & 4 deletions packages/agents/src/core/pipeline/ragPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { logger, TokenTracker } from '../../utils';
* Orchestrates the RAG process in a clear, sequential flow.
*/
export class RagPipeline {
private queryProcessor: QueryProcessor;
private documentRetriever: DocumentRetriever;
private answerGenerator: AnswerGenerator;
protected queryProcessor: QueryProcessor;
protected documentRetriever: DocumentRetriever;
protected answerGenerator: AnswerGenerator;

constructor(
private llmConfig: LLMConfig,
Expand Down Expand Up @@ -41,7 +41,7 @@ export class RagPipeline {
return emitter;
}

private async runPipeline(
protected async runPipeline(
input: RagInput,
handler: StreamHandler,
): Promise<void> {
Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/routes/cairocoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ router.post('/', async (req, res) => {
});
}

// Check for MCP mode header
const mcpMode = req.headers['mcp'] === 'true';

// Convert messages to LangChain format
const langChainMessages = convertToLangChainMessages(messages);

Expand Down Expand Up @@ -86,6 +89,7 @@ router.post('/', async (req, res) => {
llmConfig,
embeddings,
vectorStore,
mcpMode,
);

if (stream) {
Expand Down