Week 2 gave us the workbench: a Mastra project on the LiteLLM proxy. This week we build our own agent on it — the access-review agent from week 1, joining a fake HR system with real GitHub data over MCP.
Before structuring anything, two throwaway scripts prove the parts we're least sure
about (run from the repo's week2/ directory):
spike-github-mcp.ts —
can we even reach GitHub's remote MCP server? Auths with your gh CLI token
and lists the ~44 tools it exposes. (npx tsx spike-github-mcp.ts)spike-agent.ts —
the whole lesson loop in miniature: an agent with a fake-HR who_is tool plus
three GitHub tools, asked whether Priya should get repo access. It prints its verdict to
the console. (npx tsx spike-agent.ts)Tools are functions with a Zod schema on both ends. Create
src/mastra/tools/who-is.ts — a fake HR system, deliberately separate from
GitHub so the agent has to join data across two systems:
import { createTool } from '@mastra/core/tools';
import { z } from 'zod';
const ORG: Record<string, { team: string; role: string; type: string }> = {
priya: { team: 'data', role: 'analytics engineer', type: 'employee' },
jake: { team: 'marketing', role: 'campaign contractor', type: 'contractor' },
};
export const whoIs = createTool({
id: 'who_is',
description: 'Look up a person in the company HR system: team, role, employment type.',
inputSchema: z.object({ person: z.string().describe('first name, lowercase') }),
outputSchema: z.object({
found: z.boolean(),
team: z.string().optional(),
role: z.string().optional(),
type: z.string().optional(),
}),
execute: async ({ person }) => {
const rec = ORG[person.toLowerCase()];
return rec ? { found: true, ...rec } : { found: false };
},
});
We don't hand-write GitHub tools — we connect to GitHub's remote MCP server and take
theirs. Create src/mastra/tools/github-mcp.ts:
import { MCPClient } from '@mastra/mcp';
const token = process.env.GITHUB_TOKEN;
export const githubMcp = new MCPClient({
servers: {
github: {
url: new URL('https://api.githubcopilot.com/mcp/'),
requestInit: { headers: { Authorization: `Bearer ${token}` } },
},
},
timeout: 60000,
});
// The server exposes ~44 tools; hand the agent only what the job needs.
const KEEP = ['github_get_file_contents', 'github_search_repositories', 'github_get_me'];
export async function getGithubTools() {
if (!token) {
console.warn('GITHUB_TOKEN not set — access agent will run without GitHub tools.');
return {};
}
try {
const all = await githubMcp.listTools();
return Object.fromEntries(Object.entries(all).filter(([k]) => KEEP.includes(k)));
} catch (e) {
console.warn('GitHub MCP unavailable:', String(e).slice(0, 200));
return {};
}
}
Two deliberate choices: the server exposes ~44 tools and we keep 3 — a focused toolset beats a big one (less for the model to get confused by, less it can do by accident) — and if GitHub is unreachable we degrade to an agent without GitHub tools instead of crashing the whole Studio boot.
Create src/mastra/agents/access-agent.ts. The access-review policy lives in
its instructions; the model rides the same LiteLLM gateway config as week 2's weather
agent; the tools are HR + GitHub:
import { Agent } from '@mastra/core/agent';
import { whoIs } from '../tools/who-is';
import { getGithubTools } from '../tools/github-mcp';
const githubTools = await getGithubTools();
export const accessAgent = new Agent({
id: 'access-agent',
name: 'Access Agent',
description:
'Reviews GitHub repo access requests by joining HR data with the repo itself.',
instructions: `You review GitHub repo access requests for the company.
Policy:
- Team members get read access to repos their team maintains: recommend GRANT.
- Write access always needs the maintaining team's sign-off: recommend ROUTE TO OWNER.
- Admin is never automated: recommend ESCALATE.
- Contractors never get access to repos containing customer data without sign-off.
To decide, look the requester up in HR (who_is) and read the repo's README to
learn what it is and who maintains it. State your reasoning, then a one-word
verdict: GRANT, ROUTE, or ESCALATE.`,
model: {
id: `litellm/${process.env.LITELLM_MODEL ?? 'anthropic.claude-sonnet-5-global'}`,
url: process.env.LITELLM_BASE_URL,
apiKey: process.env.LITELLM_API_KEY,
},
tools: { who_is: whoIs, ...githubTools },
});
src/mastra/index.ts is the registry — anything listed here shows up in
Studio. Registration is the whole trick:
import { Mastra } from '@mastra/core/mastra';
import { accessAgent } from './agents/access-agent';
export const mastra = new Mastra({
agents: { accessAgent },
});
Add GITHUB_TOKEN to .env alongside the LiteLLM vars.
The demo target is chug2k/churn-dashboard —
a private fixture repo whose README declares the data team as maintainer.
Ask: "Priya is asking for read access to the repo chug2k/churn-dashboard. What do you recommend?"
The agent calls who_is (HR: employee, data team), then
github_get_file_contents (the repo's README: maintained by the data team),
joins the two against the policy — Verdict: GRANT