brunch

ChatGPT x Threads MCP (Mac)

by AICodeLab Tony



1) 준비물

Mac + ChatGPT 데스크탑 앱(최신)

Node.js 18+ (node -v로 확인)

Threads API 토큰 (Meta for Developers → Instagram/Threads 연동)

참고: 웹 브라우저 버전 ChatGPT에서는 MCP 직접 연결이 제한적이므로 데스크탑 앱을 사용합니다.



2) Threads 액세스 토큰 준비

Meta Developers에서 앱을 만들고 Instagram Business/Threads 계정 연결

Threads Graph API 권한 승인 후 Access Token 발급

토큰을 안전하게 보관: 프로젝트 루트의 .env 파일에 저장 예정


| THREADS_ACCESS_TOKEN=your_threads_api_token_here



3) 프로젝트 생성 (Node.js)


| 터미널에 아래 명령어 실행


mkdir threads-mcp && cd threads-mcp

npm init -y

npm i @modelcontextprotocol/sdk axios dotenv

npm i -D typescript ts-node @types/node

npx tsc --init



| package.json 스크립트 추가:


{

"type": "module",

"scripts": {

"dev": "ts-node src/server.ts"

}

}



| 폴더 생성:

mkdir -p src


| .env 파일 생성:

THREADS_ACCESS_TOKEN=PASTE_YOUR_TOKEN



4) MCP 서버 코드 (TypeScript)

src/server.ts



import dotenv from "dotenv";

import axios from "axios";

import {

StdioServerTransport,

Server,

Tool

} from "@modelcontextprotocol/sdk";


dotenv.config();


const THREADS_TOKEN = process.env.THREADS_ACCESS_TOKEN as string;

if (!THREADS_TOKEN) {

console.error("Missing THREADS_ACCESS_TOKEN in .env");

process.exit(1);

}


// 1) 도구 정의: post_to_threads

const postTool: Tool = {

name: "post_to_threads",

description: "텍스트를 Threads에 게시합니다.",

inputSchema: {

type: "object",

properties: {

text: { type: "string", description: "게시할 본문" }

},

required: ["text"]

},

async *invoke(input) {

const text = (input as any).text as string;

try {

const resp = await axios.post(

"https://graph.threads.net/v1.0/me/threads",

{ text, access_token: THREADS_TOKEN }

);

const url = resp.data?.permalink_url || resp.data?.id || "ok";

yield { content: [{ type: "text", text: `Posted: ${url}` }] };

} catch (err: any) {

yield { content: [{ type: "text", text: `Error: ${err.message}` }] };

}

}

};


// 2) MCP 서버 구동 (stdio)

const server = new Server({

name: "threads-mcp",

version: "1.0.0",

tools: [postTool]

});


const transport = new StdioServerTransport();

await server.connect(transport);




| 핵심: MCP SDK로 post_to_threads 라는 툴을 정의했고, ChatGPT가 이 툴을 호출하면 Threads Graph API에 게시합니다.



5) 실행 테스트


npm run dev



정상이라면 표준입출력(stdio)로 MCP 서버가 뜹니다. (ChatGPT가 이 프로세스를 실행/연결)



6) ChatGPT 데스크탑에 MCP 서버 등록


1. ChatGPT 데스크탑 앱 → Settings(설정) → Developer / MCP

2. Add MCP Server 선택

3. Command: npm

4. Args: run dev

(또는 node --loader ts-node/esm src/server.ts 방식 사용 가능)

5. Working Directory: 프로젝트 경로(threads-mcp 폴더)

6. Environment: THREADS_ACCESS_TOKEN=... (또는 .env 사용 시 앱에서 env 로드 옵션 사용)

7. 저장하면 ChatGPT가 서버를 구동하고 post_to_threads 툴을 발견합니다.


환경변수 주입이 어려우면, ChatGPT 설정의 MCP 항목에서 Env 키-값으로 넣어도 됩니다.



7) 사용 예시 (대화 명령)

명령어 스타일: “글 쓰레드 등록: {본문}”

ChatGPT 도구 호출 매핑 예시: 사용자가: 글 쓰레드 등록: 오늘 AI CodeLab 영상 공개! 링크는 프로필 참고 ChatGPT(도구 호출): post_to_threads({ text: "오늘 AI CodeLab 영상 공개! 링크는 프로필 참고" })

응답: Posted: https://www.threads.net/...


필요하면 시스템 프롬프트/지침에 “ 글 쓰레드 등록: ... → post_to_threads 호출” 규칙을 추가하세요.



8) 자주 발생하는 오류

401/403: 토큰 권한/만료 문제 → 새 토큰 발급, 올바른 권한 확인

429: 레이트 리밋 → 호출 간격 조절, 재시도 로직 추가

권한 실패: Instagram Business/Threads 계정 연결 및 앱 검수 상태 확인

네트워크 차단: 사내 프록시/방화벽 확인



9) 확장 팁

이미지/동영상 업로드: Threads 미디어 업로드 엔드포인트 추가 도구로 확장 (미디어 → 컨테이너 → 게시)

템플릿화: 링크/해시태그/CTA 자동 부착 도구(format_threads_text) 추가

스케줄링: 로컬 크론 또는 Make.com과 혼합 → 예약 게시 워크플로우

감지/삭제: 최근 게시물 조회/삭제 도구 추가 (list_posts, delete_post)



10) 보안

토큰은 .env와 환경변수로만 사용, 코드에 하드코딩 금지

저장소 공개(GitHub 등) 시 .env를 반드시 .gitignore 처리

장기 토큰은 주기적 롤테이션 권장



keyword
작가의 이전글동영상 텍스트 제거 방법 – 쉽게 지우는 6가지 도구