joshuamanivinod/twitter-agent-using-mcp
3.2
If you are the rightful owner of twitter-agent-using-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
The Model Context Protocol (MCP) server is a framework designed to facilitate communication between AI models and various client applications, enabling seamless integration and interaction.
Tools
1
Resources
0
Prompts
0
Created an AI agent to do twitter post on my behalf using mcp server
client
- import readline/promises
- use readline to create interface for i/o
- create chatloop to ask question(promise)
- push user,part:(text,type) question to history
- create response using await
- using @modelcontextprotocol/sdk import McpServer,SSEServerTransport
- create mcpClient
- declare tools=[] globally
- connect mcpClient to backend("/sse") using SSEClientTransport
- like this we get access to all the resourses prompts and tools of backend
- map each tool with the help of console.log("Available tools: ", tools);
- list the tools in chatloop
- to tell AI hamare paas itne tools hai, aur tum uska use kar sakte ho, we add config while taking reponse using generateContent
config:{
tools:[
{
functionDeclarations: tools
}
]
}
- shift calling chatloop() outside to inside mcpClient
- whenever ai does a function call we get to see when console.log(response.candidates[0].content) or console.log(response.candidates[0].content.parts[0])
- we need to allow ai to do the function call, how? by doing const functionCall=response.candidates[0].content.parts[0].functionCall and if a functionCall exists then call chatloop(functionCall) and in chatloop(toolCall) and then do mcpClient.callTool
- push both Calling tool and toolResult to chatHistory
- run node index.js
server
- boiler plate (run npx nodemon index.js)
import express from "express";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
const server = new McpServer({
name: "example-server",
version: "1.0.0"
});
const app = express();
const transports = {};
app.get("/sse", async (req, res) => {
const transport = new SSEServerTransport('/messages', res);
transports[ transport.sessionId ] = transport;
res.on("close", () => {
delete transports[ transport.sessionId ];
});
await server.connect(transport);
});
app.post("/messages", async (req, res) => {
const sessionId = req.query.sessionId;
const transport = transports[ sessionId ];
if (transport) {
await transport.handlePostMessage(req, res);
} else {
res.status(400).send('No transport found for sessionId');
}
});
app.listen(3001, () => {
console.log("Server is running on http://localhost:3001");
});
- create McpServer tools
- in server tool we pass func name,discription, schema, function
- use zod to specify the schema
- note: all functions inside zod are async
- function return is a bit fancy
- npm i twitter-api-v2
- create file mcp.tool.js
- create twitter account and give read and write access
- get TWITTER_APP_KEY,TWITTER_APP_SECRET,TWITTER_ACCESS_TOKEN,TWITTER_ACCESS_SECRET and store them in .env
- status contains what you are going to post which we get from client
- run server using npx nodemon index.js