Can't access auth when calling an agent from MCP

Hello, I am facing a problem happy to get your help! 🙂 Working with express, I have created the following MCP
export const startMcp = async (app: INestApplication) => {
const mastraService = app.get(MastraService, { strict: false });
const organizationService = app.get(OrganizationService, { strict: false });

const mastra = await mastraService.mastra();
const agent = mastra.getAgent('postiz');
const tools = await agent.getTools();

const server = new MCPServer({
name: 'Postiz MCP',
version: '1.0.0',
tools,
agents: { postiz: agent },
});

app.use(
'/mcp/:id',
async (req: Request, res: Response, next: NextFunction) => {
if (req.method === 'OPTIONS') {
res.sendStatus(200);
return;
}

// @ts-ignore
req.auth = await organizationService.getOrgByApiKey(req.params.id);
// @ts-ignore
if (!req.auth) {
throw new HttpException('Invalid url', 400);
}

const url = new URL(
`/mcp/${req.params.id}`,
process.env.NEXT_PUBLIC_BACKEND_URL
);
await server.startHTTP({
url,
httpPath: url.pathname,
options: {
sessionIdGenerator: () => {
return randomUUID();
},
},
req,
res,
});
}
);
};
export const startMcp = async (app: INestApplication) => {
const mastraService = app.get(MastraService, { strict: false });
const organizationService = app.get(OrganizationService, { strict: false });

const mastra = await mastraService.mastra();
const agent = mastra.getAgent('postiz');
const tools = await agent.getTools();

const server = new MCPServer({
name: 'Postiz MCP',
version: '1.0.0',
tools,
agents: { postiz: agent },
});

app.use(
'/mcp/:id',
async (req: Request, res: Response, next: NextFunction) => {
if (req.method === 'OPTIONS') {
res.sendStatus(200);
return;
}

// @ts-ignore
req.auth = await organizationService.getOrgByApiKey(req.params.id);
// @ts-ignore
if (!req.auth) {
throw new HttpException('Invalid url', 400);
}

const url = new URL(
`/mcp/${req.params.id}`,
process.env.NEXT_PUBLIC_BACKEND_URL
);
await server.startHTTP({
url,
httpPath: url.pathname,
options: {
sessionIdGenerator: () => {
return randomUUID();
},
},
req,
res,
});
}
);
};
The req.auth works great in tools, I have been doing something like this:
// @ts-ignore
if (options?.extra?.authInfo) {
// @ts-ignore
runtimeContext.set('organization', JSON.stringify(options.extra.authInfo));
}
// @ts-ignore
if (options?.extra?.authInfo) {
// @ts-ignore
runtimeContext.set('organization', JSON.stringify(options.extra.authInfo));
}
Here is where I face a problem / bug. I am using the MCP inspector from Anthropic. When I execute a tool, it works great with options?.extra?.authInfo. But if I send a question to my agent "ask_postiz", I don't get the options?.extra?.authInfo in the tool anymore. Are there any extra steps I am missing?
9 Replies
Nevo David
Nevo DavidOP6d ago
I ended up using AsyncStorage for now waiting for a response 🙂
_roamin_
_roamin_6d ago
Hey @Daniel Lew ! Is that something that's possible currently?
Mastra Triager
GitHub
[DISCORD:1428353801869332650] Can't access auth when calling an age...
This issue was created from Discord post: https://discord.com/channels/1309558646228779139/1428353801869332650 Hello, I am facing a problem happy to get your help! 🙂 Working with express, I have cr...
Daniel Lew
Daniel Lew5d ago
hmm if I understand correctly, when you use the MCPClient and pull the tool off of the MCPServer to pass to the agent, the req.auth does not get set, is that right?
Nevo David
Nevo DavidOP5d ago
Yup
Daniel Lew
Daniel Lew5d ago
is your MCP route '/mcp/:id' getting hit when the agent executes the tool?
Nevo David
Nevo DavidOP5d ago
Yes, all the other tools get the auth, only the ask<agent> after passing to the tool is not passing so for example: execute >> tool >> there is auth execute >> tool (ask<name>) - which is the agent >> tool - no auth
Daniel Lew
Daniel Lew5d ago
ooh! Now I understand, when you convert the agent to a tool it doesn't work as expected. I'm getting there is probably just some context not being passed to the agent when we convert it into a tool
Nevo David
Nevo DavidOP5d ago
Does it supposed to pass the auth, or it's not how it should work? 🙂

Did you find this page helpful?