Memory-Aware Code Review Agent (JavaScript)
Build a code review agent that remembers your team patterns and delivers consistent, context-aware feedback.
The Inconsistent Review Problem
Code reviews are essential, but they're often inconsistent. One reviewer catches naming conventions while missing error handling patterns. Another enforces error handling but ignores the team's logging standards. The result? A codebase with scattered quality and frustrated developers.
What if your code review bot could remember every pattern your team has established? What if it learned from past reviews and applied that knowledge consistently across every PR?
Let's build exactly that—a memory-aware code review agent in JavaScript using CodeMem.
Architecture Overview
Our agent combines three components: a CodeMem MCP client for persistent memory, an LLM for intelligent analysis, and a simple review engine that ties them together. The magic happens when the agent retrieves relevant team patterns before analyzing each code change.
// review-agent.ts
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import Anthropic from "@anthropic-ai/sdk";
class CodeReviewAgent {
private mcp: Client;
private llm: Anthropic;
async initialize() {
// Connect to CodeMem via MCP
const transport = new StdioClientTransport({
command: "npx",
args: ["-y", "@anthropic/codemem-mcp"]
});
this.mcp = new Client(
{ name: "code-review-agent", version: "1.0.0" },
{ capabilities: { tools: { } } }
);
await this.mcp.connect(transport);
this.llm = new Anthropic();
}
} Storing Team Patterns
Before the agent can remember patterns, we need to store them. Each memory captures a specific team convention with enough context for semantic retrieval later.
async storePattern(pattern: string, category: string) {
await this.mcp.callTool("store_memory", {
content: pattern,
tags: ["code-review", category, "team-pattern"]
});
}
// Example patterns to store
await agent.storePattern(
"Always use async/await over .then() chains for better readability",
"async-patterns"
);
await agent.storePattern(
"Wrap external API calls in try-catch with specific error types: " +
"NetworkError, TimeoutError, ValidationError",
"error-handling"
);
await agent.storePattern(
"Use zod for runtime validation at API boundaries. Schema files " +
"go in src/schemas/ with .schema.ts suffix",
"validation"
); Context-Aware Reviews
Here's where memory transforms the review process. Before analyzing code, the agent searches for relevant patterns based on what the code actually does. An async function triggers async pattern memories. API calls surface error handling conventions.
async reviewCode(code: string, filename: string): Promise<Review> {
// Step 1: Identify what patterns might be relevant
const codeContext = await this.analyzeCodeContext(code);
// Step 2: Retrieve matching team patterns from memory
const patterns = await this.mcp.callTool("search_memories", {
query: `code review patterns for: ${codeContext}`,
tags: ["team-pattern"],
limit: 5
});
// Step 3: Review with full context
const response = await this.llm.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
messages: [{
role: "user",
content: `Review this code against our team patterns:
## Team Patterns
${patterns.content.map(p => `- ${p.content}`).join('\n')}
## Code to Review (${filename})
\`\`\`
${code}
\`\`\`
Identify violations and suggest improvements.`
}]
});
return this.parseReview(response);
} Learning from Reviews
The best part? The agent learns from every review. When a senior developer adds a comment about a pattern the agent missed, we capture it as a new memory. The next review will include that knowledge.
async learnFromFeedback(feedback: string, codeContext: string) {
// Store human feedback as new patterns
const pattern = await this.llm.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 256,
messages: [{
role: "user",
content: `Extract a reusable code review pattern from this feedback:
Context: ${codeContext}
Feedback: ${feedback}
Write a concise, actionable pattern (1-2 sentences).`
}]
});
await this.storePattern(pattern.content[0].text, "learned");
} Why Memory Changes Everything
Without memory, every code review starts from scratch. The AI knows general best practices but nothing about your team's specific conventions. You end up explaining the same things repeatedly—or worse, the bot gives generic advice that conflicts with your established patterns.
With CodeMem, the agent builds institutional knowledge over time. It remembers that your team prefers Zod over Joi. It knows you've standardized on a specific error hierarchy. It recalls that three months ago, you decided to avoid barrel exports for tree-shaking reasons.
The result? Reviews that feel like they come from a team member who actually knows your codebase, not a generic linter with an LLM attached.
Getting Started
You can implement this pattern in under an hour. The CodeMem MCP server handles all the vector storage and semantic search. You focus on the review logic and pattern extraction.
Start by storing your team's top 10 conventions. Run the agent on a few PRs. Watch it catch issues that would have slipped through. Then connect the feedback loop so it keeps learning.
This is just one use case for persistent AI memory. The same pattern applies to documentation agents, onboarding bots, or any AI tool that benefits from knowing your team's context.
Ready to Build Memory-Aware Agents?
CodeMem gives your AI tools persistent memory through MCP. Store patterns, retrieve context, and build agents that actually remember.
Get Started with CodeMem →