# From Chatbot to Code Guru: Building the TelexLinter AI Agent with Mastra and the A2A Protocol

### Introduction: The Stage 3 Mandate

The challenge for HNG Stage 3 was clear: build a useful AI agent and integrate it with the [**Telex.im**](http://Telex.im) platform. We needed to move past simple API calls and demonstrate true **agentic design**, which meant equipping our agent with tools, context, and robust integration protocols.

I chose to build the **TelexLinter**, a real-time code review agent that helps developers catch security flaws and outdated APIs right in their project chat.

### Core Architecture: The Mastra Agent

Our agent, named `TelexLinter`, was defined using the **Mastra (TypeScript)** framework. Mastra allowed us to consolidate our agent's brain, memory, and tools into a single, cohesive unit.

The heart of the agent is its **System Prompt** (the `instructions` field). This prompt enforces a strict protocol:

1. **Tool Execution:** Always call the custom tools first.
    
2. **Structured Review:** Deliver findings in a concise bulleted list.
    
3. **Final Output:** Provide a clean, executable **refactored code block**.
    

### The Innovation: Custom Tools (The Value-Add)

A core part of the submission was showing the agent could perform deterministic tasks beyond the LLM's general knowledge. We achieved this by building two custom tools using Mastra's `createTool` function:

1. `securityScannerTool`:
    
    * **Purpose:** Static analysis for immediate security risks.
        
    * **Logic:** Simple TypeScript logic using string searches to flag hardcoded secrets (`API_KEY=`) and dangerous functions (`eval()`, unsafe SQL string concatenation).
        
2. `packageDocsLookupTool`:
    
    * **Purpose:** A simulated **RAG** (Retrieval-Augmented Generation) lookup for best practices.
        
    * **Logic:** Checks if the submitted code contains known deprecated functions (e.g., Express `body-parser` or old React lifecycle methods) and returns the modern replacement guidance.
        

By explicitly telling the LLM to run these tools before forming an answer, we ensured the **TelexLinter** provided factual, authoritative advice.

### The Integration Challenge: Conquering the A2A Protocol

The most complex technical task was integrating the Mastra agent with the [Telex.im](http://Telex.im) platform, which uses the **A2A (Agent-to-Agent) Protocol**—a standard built on **JSON-RPC 2.0**.

#### The Custom A2A Gateway

Instead of using a generic endpoint, I built a custom **A2A Gateway** using Mastra's `registerApiRoute`. This endpoint acts as a translator between Telex and our agent.

The custom handler performs the following critical steps in TypeScript:

1. **Protocol Validation:** It immediately verifies the incoming request for the mandatory `jsonrpc: "2.0"` and the `id` field.
    
2. **Request Mapping:** It converts the A2A message structure (which uses `parts` for content) into the simple `[ {role: "user", content: "..."} ]` format Mastra expects.
    
3. **Agent Execution:** It calls `mastra.getAgent('TelexLinter').generate(mastraMessages)`.
    
4. **Response Mapping:** It packages the agent's final text and history into a compliant A2A **Task Object** with the state set to `"completed"`, guaranteeing the message is delivered correctly to the user on Telex.
    

### The Final Workflow Configuration

The entire solution is connected via the Telex Workflow JSON. This JSON simply points the platform's execution node to our custom public endpoint:

JSON

```plaintext
{
  "type": "a2a/mastra-a2a-node",
  "url": "https://shrilling-sparse-cartoon.mastra.cloud/a2a/agent/TelexLinter" 
  /* ... rest of config ... */
}
```

This single-node configuration proved that all the heavy lifting validation, translation, and tool orchestration, was correctly isolated within the highly functional Mastra agent.

### Conclusion

Building the **TelexLinter** was a massive leap from standard CRUD apps. It required mastering agent logic, implementing tool-calling workflows, and, critically, solving multi-protocol communication using a custom gateway.

I'm proud of this solution for demonstrating not just a working agent, but a truly robust and scalable backend system. Try the **TelexLinter** on [Telex.im](http://Telex.im) today!

---
