loading…
Search for a command to run...
loading…
A template for building MCP servers that supports both HTTP transport with Bearer token authentication and stdio-based connections for local clients like Codex
A template for building MCP servers that supports both HTTP transport with Bearer token authentication and stdio-based connections for local clients like Codex and Claude. It provides a foundational structure for practicing secure authentication and tool integration within the Model Context Protocol.
실습용 MCP 템플릿입니다.
이 프로젝트는 두 가지 목적을 같이 다룹니다.
HTTP 기반 MCP 서버에서 인증이 어떻게 붙는지 연습stdio 기반 MCP 서버를 Codex 같은 로컬 MCP 클라이언트에 연결현재 포함된 기능은 최소 구성입니다.
GET /healthzPOST /mcp with Bearer tokenstdio MCP entrypointhello tool 1개핵심 파일은 아래와 같습니다.
src/mcp-server.jshello tool 등록src/server.js/healthz/mcp Bearer 인증src/stdio.js툴을 추가하는 방법은 별도 문서로 분리했습니다.
npm install
cp .env.example .env
기본값은 아래와 같습니다.
PORT=3000
HOST=127.0.0.1
MCP_BEARER_TOKEN=dev-token
npm start
또는:
npm run start:http
실행 후:
http://127.0.0.1:3000/healthzhttp://127.0.0.1:3000/mcpnpm run start:stdio
이 방식은 포트를 열지 않습니다.
로컬 MCP 클라이언트가 프로세스를 직접 실행해서 stdin/stdout으로 통신합니다.
Authorization: Bearer <token> 필요중요한 차이:
HTTP: 네가 서버를 먼저 실행stdio: Codex가 필요할 때 서버를 실행/mcp 요청은 모두 아래 헤더가 필요합니다.
Authorization: Bearer dev-token
값은 .env의 MCP_BEARER_TOKEN으로 변경할 수 있습니다.
인증 실패 시:
401 UnauthorizedWWW-Authenticate 헤더 반환curl -i http://127.0.0.1:3000/healthz
curl -i \
-X POST http://127.0.0.1:3000/mcp \
-H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-H 'Authorization: Bearer dev-token' \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-11-05",
"capabilities": {},
"clientInfo": {
"name": "curl-client",
"version": "0.1.0"
}
}
}'
응답 헤더의 Mcp-Session-Id 값을 다음 요청에 재사용합니다.
curl -i \
-X POST http://127.0.0.1:3000/mcp \
-H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-H 'Authorization: Bearer dev-token' \
-H 'Mcp-Session-Id: <session-id>' \
-d '{
"jsonrpc": "2.0",
"method": "notifications/initialized",
"params": {}
}'
curl -i \
-X POST http://127.0.0.1:3000/mcp \
-H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-H 'Authorization: Bearer dev-token' \
-H 'Mcp-Session-Id: <session-id>' \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/list",
"params": {}
}'
curl -i \
-X POST http://127.0.0.1:3000/mcp \
-H 'Content-Type: application/json' \
-H 'Accept: application/json, text/event-stream' \
-H 'Authorization: Bearer dev-token' \
-H 'Mcp-Session-Id: <session-id>' \
-d '{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "hello",
"arguments": {
"name": "MCP"
}
}
}'
예상 응답 예시:
{
"result": {
"content": [
{
"type": "text",
"text": "Hello, MCP! This response came from a authenticated HTTP MCP tool at 2026-03-18T05:06:22.258Z."
}
],
"structuredContent": {
"greeting": "Hello, MCP!",
"accessMode": "http",
"server": "mcp-auth-template",
"timestamp": "2026-03-18T05:06:22.258Z"
}
},
"jsonrpc": "2.0",
"id": 3
}
이 프로젝트를 Codex에 붙일 때는 HTTP가 아니라 stdio를 사용합니다.
이유:
~/.codex/config.toml에 아래를 추가합니다.
[mcp_servers.hello_local]
command = "node"
args = ["/Users/leeseungchan/develop/mcp/src/stdio.js"]
이 프로젝트에서 Codex를 바로 쓰려면 trusted project 설정도 있으면 편합니다.
[projects."/Users/leeseungchan/develop/mcp"]
trust_level = "trusted"
cd /Users/leeseungchan/develop/mcp
codex
Codex에 아래처럼 물어보면 됩니다.
사용 가능한 MCP 도구 보여줘
hello_local의 hello 툴 호출해줘
hello_local의 hello 툴을 name=MCP 로 호출해줘
npm run start:stdio를 미리 켜둘 필요는 없음node /Users/leeseungchan/develop/mcp/src/stdio.js를 직접 실행이 프로젝트를 Claude 쪽에 붙일 때도 stdio 방식을 쓰는 편이 가장 단순합니다.
Anthropic 공식 문서 기준으로 Claude Code는 로컬 stdio MCP 서버를 아래 형태로 추가할 수 있습니다.
claude mcp add hello-local -- node /Users/leeseungchan/develop/mcp/src/stdio.js
추가 후 확인:
claude mcp list
세부 설정 보기:
claude mcp get hello-local
이후 Claude Code를 실행한 뒤 다음처럼 요청하면 됩니다.
사용 가능한 MCP 도구 보여줘
hello-local의 hello 툴 호출해줘
Anthropic 공식 문서에는 Claude Desktop에서 claude_desktop_config.json의 mcpServers 항목으로 MCP 서버를 등록하는 예시가 나옵니다.
이 프로젝트를 붙일 때는 아래처럼 넣으면 됩니다.
{
"mcpServers": {
"hello-local": {
"command": "node",
"args": ["/Users/leeseungchan/develop/mcp/src/stdio.js"],
"env": {}
}
}
}
Claude Desktop을 다시 열고 아래처럼 물어보면 됩니다.
사용 가능한 MCP 도구 보여줘
hello-local의 hello 툴을 name=Claude 로 호출해줘
HTTP가 아니라 stdio 사용을 권장npm run start:stdio를 미리 켜둘 필요 없음node /Users/leeseungchan/develop/mcp/src/stdio.js를 직접 실행--scope 또는 .mcp.json 방식을 쓰는 편이 좋음직접 확인한 동작은 아래입니다.
GET /healthz 정상 응답/mcp 요청에 401initializetools/listtools/callinitialize -> notifications/initialized -> tools/list -> tools/call~/.codex/config.toml에 hello_local 등록이 템플릿은 여기서 확장하면 됩니다.
hello 말고 툴 추가JWT 검증으로 교체OAuth 기반 인증으로 확장resources / prompts 추가src/server.jssrc/mcp-server.jssrc/stdio.jsДобавь это в claude_desktop_config.json и перезапусти Claude Desktop.
{
"mcpServers": {
"mcp-auth-template": {
"command": "npx",
"args": []
}
}
}