콘텐츠로 이동

모델 컨텍스트 프로토콜 (MCP)

Model Context Protocol (MCP)는 애플리케이션이 LLM에 도구와 컨텍스트를 제공하는 방식을 표준화하는 오픈 프로토콜입니다. MCP 문서에서는 다음과 같이 설명합니다.

MCP는 애플리케이션이 LLM에 컨텍스트를 제공하는 방식을 표준화하는 오픈 프로토콜입니다. MCP를 AI 애플리케이션용 USB-C 포트라고 생각하면 됩니다. USB-C가 다양한 주변기기와 액세서리에 장치를 연결하는 표준화된 방법을 제공하듯, MCP는 AI 모델을 다양한 데이터 소스와 도구에 연결하는 표준화된 방법을 제공합니다.

이 SDK가 지원하는 MCP 서버는 세 가지 유형입니다.

  1. 호스티드 MCP 서버 도구 - OpenAI Responses API에서 도구로 사용하는 원격 MCP 서버
  2. Streamable HTTP MCP 서버 - Streamable HTTP 전송을 구현한 로컬 또는 원격 서버
  3. Stdio MCP 서버 - 표준 입출력으로 접근하는 서버(가장 단순한 옵션)

참고: SDK에는 레거시 Server-Sent Events 전송을 위한 MCPServerSSE도 포함되어 있지만, SSE는 MCP 프로젝트에서 더 이상 권장되지 않습니다. 새 연동에서는 Streamable HTTP 또는 stdio를 권장합니다.

사용 사례에 따라 서버 유형을 선택하세요.

필요한 것권장 옵션
기본 OpenAI responses 모델로 공개 접근 가능한 원격 서버 호출1. 호스티드 MCP 도구
공개 접근 가능한 원격 서버를 사용하되 도구 호출은 로컬에서 트리거2. Streamable HTTP
로컬에서 실행 중인 Streamable HTTP 서버 사용2. Streamable HTTP
OpenAI-Responses가 아닌 모델에서 Streamable HTTP 서버 사용2. Streamable HTTP
표준 I/O 프로토콜만 지원하는 로컬 MCP 서버 사용3. Stdio

호스티드 툴은 전체 왕복 과정을 모델 내부로 밀어 넣습니다. 코드가 MCP 서버를 호출하는 대신, OpenAI Responses API가 원격 도구 엔드포인트를 호출하고 결과를 모델로 스트리밍합니다.

다음은 호스티드 MCP 도구를 사용하는 가장 간단한 예제입니다. hostedMcpTool 유틸리티 함수에 원격 MCP 서버의 레이블과 URL을 전달할 수 있으며, 호스티드 MCP 서버 도구를 만드는 데 유용합니다.

hostedAgent.ts
import { Agent, hostedMcpTool } from '@openai/agents';
export const agent = new Agent({
name: 'MCP Assistant',
instructions: 'You must always use the MCP tools to answer questions.',
tools: [
hostedMcpTool({
serverLabel: 'gitmcp',
serverUrl: 'https://gitmcp.io/openai/codex',
}),
],
});

그런 다음 run 함수(또는 사용자 정의 Runner 인스턴스의 run 메서드)로 Agent를 실행할 수 있습니다.

Run with hosted MCP tools
import { run } from '@openai/agents';
import { agent } from './hostedAgent';
async function main() {
const result = await run(
agent,
'Which language is the repo I pointed in the MCP tool settings written in?',
);
console.log(result.finalOutput);
}
main().catch(console.error);

증분 MCP 결과를 스트리밍하려면 Agent를 실행할 때 stream: true를 전달하세요.

Run with hosted MCP tools (streaming)
import { isOpenAIResponsesRawModelStreamEvent, run } from '@openai/agents';
import { agent } from './hostedAgent';
async function main() {
const result = await run(
agent,
'Which language is the repo I pointed in the MCP tool settings written in?',
{ stream: true },
);
for await (const event of result) {
if (
isOpenAIResponsesRawModelStreamEvent(event) &&
event.data.event.type !== 'response.mcp_call_arguments.delta' &&
event.data.event.type !== 'response.output_text.delta'
) {
console.log(`Got event of type ${JSON.stringify(event.data)}`);
}
}
console.log(`Done streaming; final result: ${result.finalOutput}`);
}
main().catch(console.error);

민감한 작업의 경우 개별 도구 호출에 대해 사람의 승인을 요구할 수 있습니다. requireApproval: 'always'를 전달하거나, 도구 이름을 'never'/'always'에 매핑하는 세밀한 객체를 전달하세요.

도구 호출이 안전한지 프로그래밍 방식으로 판단할 수 있다면 onApproval 콜백을 사용해 도구 호출을 승인하거나 거부할 수 있습니다. 사람의 승인이 필요하다면 로컬 함수 도구에서와 마찬가지로 interruptions를 사용하는 동일한 휴먼 인 더 루프 (HITL) 접근 방식을 사용할 수 있습니다.

Human in the loop with hosted MCP tools
import { Agent, run, hostedMcpTool, RunToolApprovalItem } from '@openai/agents';
async function main(): Promise<void> {
const agent = new Agent({
name: 'MCP Assistant',
instructions: 'You must always use the MCP tools to answer questions.',
tools: [
hostedMcpTool({
serverLabel: 'gitmcp',
serverUrl: 'https://gitmcp.io/openai/codex',
// 'always' | 'never' | { never, always }
requireApproval: {
never: {
toolNames: ['search_codex_code', 'fetch_codex_documentation'],
},
always: {
toolNames: ['fetch_generic_url_content'],
},
},
}),
],
});
let result = await run(agent, 'Which language is this repo written in?');
while (result.interruptions && result.interruptions.length) {
for (const interruption of result.interruptions) {
// Human in the loop here
const approval = await confirm(interruption);
if (approval) {
result.state.approve(interruption);
} else {
result.state.reject(interruption);
}
}
result = await run(agent, result.state);
}
console.log(result.finalOutput);
}
import { stdin, stdout } from 'node:process';
import * as readline from 'node:readline/promises';
async function confirm(item: RunToolApprovalItem): Promise<boolean> {
const rl = readline.createInterface({ input: stdin, output: stdout });
const name = item.name;
const params = item.arguments;
const answer = await rl.question(
`Approve running tool (mcp: ${name}, params: ${params})? (y/n) `,
);
rl.close();
return answer.toLowerCase().trim() === 'y';
}
main().catch(console.error);

hostedMcpTool(...)는 MCP 서버 URL과 커넥터 기반 서버를 모두 지원합니다.

OptionTypeNotes
serverLabelstring이벤트와 트레이스에서 호스티드 MCP 서버를 식별하는 필수 레이블
serverUrlstring원격 MCP 서버 URL(일반 호스티드 MCP 서버에 사용)
connectorIdstringOpenAI connector id(serverUrl 대신 커넥터 기반 호스티드 서버에 사용)
authorizationstring호스티드 MCP 백엔드로 전송되는 선택적 인증 토큰
headersRecord<string, string>선택적 추가 요청 헤더
allowedToolsstring[] | object모델에 노출할 도구 이름 허용 목록. string[] 또는 { toolNames?: string[] } 전달
deferLoadingboolean호스티드 MCP 도구의 Responses 전용 지연 로딩. 같은 에이전트에 toolSearchTool() 필요
requireApproval'never' | 'always' | object호스티드 MCP 도구 호출의 승인 정책. 도구별 재정의에는 객체 형태 사용. 기본값은 'never'
onApprovalApproval callbackrequireApproval이 승인 처리를 요구할 때 프로그래밍 방식 승인/거부를 위한 선택적 콜백

모델이 호스티드 MCP 서버의 도구 정의를 처음부터 노출하는 대신 tool search를 통해 필요 시 로드하도록 하려면 deferLoading: true를 설정하세요. 이는 OpenAI Responses API에서만 동작하고, 같은 요청에 toolSearchTool()가 필요하며, GPT-5.4 이상 지원 모델 릴리스에서 사용해야 합니다. 전체 지연 로딩 설정은 도구 가이드를 참고하세요.

requireApproval 객체 형태:

{
always?: { toolNames: string[] };
never?: { toolNames: string[] };
}

onApproval 시그니처:

async function onApproval(
context,
item,
): Promise<{
approve: boolean;
reason?: string;
}> {}

호스티드 MCP는 OpenAI 커넥터도 지원합니다. serverUrl 대신 커넥터의 connectorIdauthorization 토큰을 전달하세요. 그러면 Responses API가 인증을 처리하고, 호스티드 MCP 인터페이스를 통해 커넥터 도구를 노출합니다.

Connector-backed hosted MCP tool
import { Agent, hostedMcpTool } from '@openai/agents';
const authorization = process.env.GOOGLE_CALENDAR_AUTHORIZATION!;
export const connectorAgent = new Agent({
name: 'Calendar Assistant',
instructions:
"You are a helpful assistant that can answer questions about the user's calendar.",
tools: [
hostedMcpTool({
serverLabel: 'google_calendar',
connectorId: 'connector_googlecalendar',
authorization,
requireApproval: 'never',
}),
],
});

이 예제에서 GOOGLE_CALENDAR_AUTHORIZATION 환경 변수에는 Google OAuth Playground에서 획득한 OAuth 토큰이 들어 있으며, 이 토큰으로 커넥터 기반 서버가 Calendar API를 호출하도록 인증합니다. 스트리밍까지 포함한 실행 가능한 샘플은 examples/connectors를 참고하세요.

완전히 동작하는 샘플(호스티드 툴/Streamable HTTP/stdio + 스트리밍, HITL, onApproval)은 GitHub 리포지토리의 examples/mcp에 있습니다.

Agent가 로컬 또는 원격 Streamable HTTP MCP 서버와 직접 통신할 때는 서버의 url, name, 그리고 선택 설정으로 MCPServerStreamableHttp를 생성하세요.

Run with Streamable HTTP MCP servers
import { Agent, run, MCPServerStreamableHttp } from '@openai/agents';
async function main() {
const mcpServer = new MCPServerStreamableHttp({
url: 'https://gitmcp.io/openai/codex',
name: 'GitMCP Documentation Server',
});
const agent = new Agent({
name: 'GitMCP Assistant',
instructions: 'Use the tools to respond to user requests.',
mcpServers: [mcpServer],
});
try {
await mcpServer.connect();
const result = await run(agent, 'Which language is this repo written in?');
console.log(result.finalOutput);
} finally {
await mcpServer.close();
}
}
main().catch(console.error);

생성자 옵션:

OptionTypeNotes
urlstringStreamable HTTP 서버 URL
namestring서버의 선택적 레이블
cacheToolsListboolean지연 시간 감소를 위한 도구 목록 캐시
clientSessionTimeoutSecondsnumberMCP 클라이언트 세션 타임아웃
toolFilterMCPToolFilterCallable | MCPToolFilterStatic사용 가능한 도구 필터링
toolMetaResolverMCPToolMetaResolver호출별 MCP _meta 요청 필드 주입
errorFunctionMCPToolErrorFunction | nullMCP 호출 실패를 모델에 보이는 텍스트로 매핑
timeoutnumber요청별 타임아웃(밀리초)
loggerLogger사용자 정의 로거
authProviderOAuthClientProviderMCP TypeScript SDK의 OAuth provider
requestInitRequestInit요청용 Fetch 초기화 옵션
fetchFetchLike사용자 정의 fetch 구현
reconnectionOptionsStreamableHTTPReconnectionOptions재연결 튜닝 옵션
sessionIdstringMCP 연결용 명시적 세션 id

생성자는 authProvider, requestInit, fetch, reconnectionOptions, sessionId 같은 추가 MCP TypeScript-SDK 옵션도 받습니다. 자세한 내용은 MCP TypeScript SDK 저장소와 해당 문서를 참고하세요.

표준 I/O만 노출하는 서버의 경우 fullCommandMCPServerStdio를 생성하세요.

Run with Stdio MCP servers
import { Agent, run, MCPServerStdio } from '@openai/agents';
import * as path from 'node:path';
async function main() {
const samplesDir = path.join(__dirname, 'sample_files');
const mcpServer = new MCPServerStdio({
name: 'Filesystem MCP Server, via npx',
fullCommand: `npx -y @modelcontextprotocol/server-filesystem ${samplesDir}`,
});
await mcpServer.connect();
try {
const agent = new Agent({
name: 'FS MCP Assistant',
instructions:
'Use the tools to read the filesystem and answer questions based on those files. If you are unable to find any files, you can say so instead of assuming they exist.',
mcpServers: [mcpServer],
});
const result = await run(agent, 'Read the files and list them.');
console.log(result.finalOutput);
} finally {
await mcpServer.close();
}
}
main().catch(console.error);

생성자 옵션:

OptionTypeNotes
command / argsstring / string[]stdio 서버용 명령어 + 인자
fullCommandstringcommand + args 대신 사용할 전체 명령 문자열
envRecord<string, string>서버 프로세스용 환경 변수
cwdstring서버 프로세스 작업 디렉터리
cacheToolsListboolean지연 시간 감소를 위한 도구 목록 캐시
clientSessionTimeoutSecondsnumberMCP 클라이언트 세션 타임아웃
namestring서버의 선택적 레이블
encodingstringstdio 스트림 인코딩
encodingErrorHandler'strict' | 'ignore' | 'replace'인코딩 오류 처리 전략
toolFilterMCPToolFilterCallable | MCPToolFilterStatic사용 가능한 도구 필터링
toolMetaResolverMCPToolMetaResolver호출별 MCP _meta 요청 필드 주입
errorFunctionMCPToolErrorFunction | nullMCP 호출 실패를 모델에 보이는 텍스트로 매핑
timeoutnumber요청별 타임아웃(밀리초)
loggerLogger사용자 정의 로거

여러 MCP 서버를 함께 사용할 때는 connectMcpServers를 사용해 한곳에서 연결, 실패 추적, 종료를 관리할 수 있습니다. 이 헬퍼는 active, failed, errors 컬렉션을 가진 MCPServers 인스턴스를 반환하므로, 정상 서버만 에이전트에 전달할 수 있습니다.

Manage multiple MCP servers
import {
Agent,
MCPServerStreamableHttp,
connectMcpServers,
run,
} from '@openai/agents';
async function main() {
const servers = [
new MCPServerStreamableHttp({
url: 'https://mcp.deepwiki.com/mcp',
name: 'DeepWiki MCP Server',
}),
new MCPServerStreamableHttp({
url: 'http://localhost:8001/mcp',
name: 'Local MCP Server',
}),
];
const mcpServers = await connectMcpServers(servers, {
connectInParallel: true,
});
try {
console.log(`Active servers: ${mcpServers.active.length}`);
console.log(`Failed servers: ${mcpServers.failed.length}`);
for (const [server, error] of mcpServers.errors) {
console.warn(`${server.name} failed to connect: ${error.message}`);
}
const agent = new Agent({
name: 'MCP lifecycle agent',
instructions: 'Use MCP tools to answer user questions.',
mcpServers: mcpServers.active,
});
const result = await run(
agent,
'Which language is the openai/codex repository written in?',
);
console.log(result.finalOutput);
} finally {
await mcpServers.close();
}
}
main().catch(console.error);

사용 사례:

  • 여러 서버 동시 사용: 모든 서버를 병렬로 연결하고 에이전트에는 mcpServers.active를 사용
  • 부분 실패 처리: failed + errors를 확인해 계속 진행할지 재시도할지 결정
  • 실패 서버 재시도: mcpServers.reconnect() 호출(기본값은 실패 서버만 재시도)

엄격한 “전부 성공 아니면 실패” 연결이나 다른 타임아웃이 필요하면 connectMcpServers(servers, options)를 사용해 환경에 맞게 옵션을 조정하세요.

connectMcpServers 옵션:

OptionTypeDefaultNotes
connectTimeoutMsnumber | null10000각 서버 connect() 타임아웃. 비활성화하려면 null 사용
closeTimeoutMsnumber | null10000각 서버 close() 타임아웃. 비활성화하려면 null 사용
dropFailedbooleantrue실패 서버를 active에서 제외
strictbooleanfalse서버 하나라도 연결 실패 시 예외 발생
suppressAbortErrorbooleantrue실패 서버 추적은 유지하면서 abort 유사 오류 무시
connectInParallelbooleanfalse순차 대신 모든 서버를 동시에 연결

mcpServers.reconnect(options) 지원 항목:

OptionTypeDefaultNotes
failedOnlybooleantrue실패 서버만 재시도(true) 또는 전체 서버 재연결(false)

런타임이 Symbol.asyncDispose를 지원하면 MCPServersawait using 패턴도 지원합니다. TypeScript에서는 tsconfig.jsonesnext.disposable을 활성화하세요.

{
"compilerOptions": {
"lib": ["ES2018", "DOM", "esnext.disposable"]
}
}

그다음 다음과 같이 작성할 수 있습니다.

await using mcpServers = await connectMcpServers(servers);

Streamable HTTPStdio 서버의 경우 Agent가 실행될 때마다 사용 가능한 도구를 찾기 위해 list_tools()를 호출할 수 있습니다. 이 왕복은 지연 시간을 늘릴 수 있으므로(특히 원격 서버), MCPServerStdio 또는 MCPServerStreamableHttpcacheToolsList: true를 전달해 결과를 메모리에 캐시할 수 있습니다.

도구 목록이 바뀌지 않는다고 확신할 때만 활성화하세요. 나중에 캐시를 무효화하려면 서버 인스턴스에서 invalidateToolsCache()를 호출하세요. getAllMcpTools(...)를 통한 공유 MCP 도구 캐시를 사용하는 경우 invalidateServerToolsCache(serverName)로 서버 이름 기준 무효화도 가능합니다.

고급 사용 사례에서는 getAllMcpTools({ generateMCPToolCacheKey })를 통해 캐시 분할(예: 서버 + 에이전트 + 실행 컨텍스트 기준)을 사용자 정의할 수 있습니다.

각 서버에서 노출할 도구를 제한하려면 createMCPToolStaticFilter를 통한 정적 필터 또는 사용자 정의 함수를 전달하세요. 아래는 두 방식을 함께 보여주는 예제입니다.

Tool filtering
import {
MCPServerStdio,
MCPServerStreamableHttp,
createMCPToolStaticFilter,
MCPToolFilterContext,
} from '@openai/agents';
interface ToolFilterContext {
allowAll: boolean;
}
const server = new MCPServerStdio({
fullCommand: 'my-server',
toolFilter: createMCPToolStaticFilter({
allowed: ['safe_tool'],
blocked: ['danger_tool'],
}),
});
const dynamicServer = new MCPServerStreamableHttp({
url: 'http://localhost:3000',
toolFilter: async ({ runContext }: MCPToolFilterContext, tool) =>
(runContext.context as ToolFilterContext).allowAll || tool.name !== 'admin',
});