JSON 웹 토큰
그만큼MastraJwtAuth클래스는 JWT(JSON 웹 토큰)를 사용하여 Mastra에 대한 경량 인증 메커니즘을 제공합니다. 공유 비밀을 기반으로 들어오는 요청을 확인하고 다음을 사용하여 Mastra 서버와 통합합니다.auth옵션.
설치설치에 대한 직접 링크
사용하기 전에MastraJwtAuth class you have to install the @mastra/auth package.
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/auth@latest
pnpm add @mastra/auth@latest
yarn add @mastra/auth@latest
bun add @mastra/auth@latest
JWT 만들기JWT 만들기에 대한 직접 링크
Mastra 서버에 대한 요청을 인증하려면 다음으로 서명된 유효한 JSON 웹 토큰(JWT)이 필요합니다.MASTRA_JWT_SECRET.
생성하는 가장 쉬운 방법은 다음을 사용하는 것입니다.jwt.io:
- 선택하다JWT Encoder.
- 아래로 스크롤하여Sign JWT: Secret section.
- 비밀번호를 입력하세요(예:
supersecretdevkeythatishs256safe!). - 딸깍 하는 소리Generate example to create a valid JWT.
- 생성된 토큰을 복사하여 다음과 같이 설정합니다.
MASTRA_JWT_TOKENin your.envfile.
사용예사용예에 대한 직접 링크
생성된 JWT를 가져와 구성에 사용합니다.MastraJwtAuth in your Mastra server:
import { Mastra } from '@mastra/core'
import { MastraJwtAuth } from '@mastra/auth'
export const mastra = new Mastra({
server: {
auth: new MastraJwtAuth({
secret: process.env.MASTRA_JWT_SECRET,
}),
},
})
방문하다MastraJwtAuth for all available configuration options.
내부에Studio, go to Settings and under Headers select the "Add Header" button. Enter Authorization as the header name and Bearer <your-jwt> as the value.
구성MastraClientconfiguring-mastraclient에 대한 직접 링크
언제auth is enabled, all requests made with MastraClient must include a valid JWT in the Authorization header:
import { MastraClient } from '@mastra/client-js'
export const mastraClient = new MastraClient({
baseUrl: 'https://<mastra-api-url>',
headers: {
Authorization: `Bearer ${process.env.MASTRA_JWT_TOKEN}`,
},
})
방문하다Mastra Client SDK for more configuration options.
인증된 요청 만들기인증된 요청 만들기에 대한 직접 링크
한 번MastraClient 가 구성되면 프런트엔드 애플리케이션에서 인증된 요청을 보내거나 다음을 사용할 수 있습니다: curl for quick local testing:
- React
- cURL
import { mastraClient } from '../../lib/mastra-client'
export const TestAgent = () => {
async function handleClick() {
const agent = mastraClient.getAgent('weatherAgent')
const response = await agent.generate('Weather in London')
console.log(response)
}
return <button onClick={handleClick}>Test Agent</button>
}
curl -X POST http://localhost:4111/api/agents/weatherAgent/generate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <your-jwt>" \
-d '{
"messages": "Weather in London"
}'