Google Sheets
Connect to Google sheets using the composio mcp
Last updated
Connect to Google sheets using the composio mcp
Last updated
Here is an example that connects to a remote Google Sheets MCP server hosted by composio.
This example requires a complex flow where it requires connecting your google account to composio to access google sheets. Then, HyperAgent will browser Wikipedia to get population statistics for US states, and insert them into a Google Sheet.
import dotenv from "dotenv";
import chalk from "chalk";
import { ChatOpenAI } from "@langchain/openai";
import HyperAgent from "@hyperbrowser/agent";
dotenv.config();
const TASK = `1. Run GOOGLESHEETS_CHECK_ACTIVE_CONNECTION to check if there is an active connection.
2. If there is an active connection, go to 4. Otherwise, go to 3.
3. Run GOOGLESHEETS_INITIATE_CONNECTION and output the the auth link to the user, then wait for the connection to be active.
4. Create a new spreadsheet titled "Most Populated States".
5. Go to https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population and get the data on the top 5 most populous states from the table.
6. Add that information to the spreadsheet properly.
Make sure that the data is well formatted and the columns are all there.`;
async function run(mcpUrl: string) {
console.log(chalk.cyan.bold("\n===== Running Task ====="));
console.log(chalk.white(`Task: ${TASK}`));
console.log(chalk.cyan.bold("=======================\n"));
console.log(chalk.yellow("Initializing OpenAI LLM..."));
const llm = new ChatOpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4o",
});
console.log(chalk.yellow("Creating HyperAgent..."));
try {
const agent = new HyperAgent({
llm: llm,
debug: true,
});
console.log(chalk.green("Agent created successfully"));
console.log(
chalk.yellow("Connecting to Composio Googlesheets MCP server...")
);
await agent.initializeMCPClient({
servers: [
{
command: "npx",
args: ["@composio/mcp@latest", "start", "--url", mcpUrl],
env: {
npm_config_yes: "true",
},
},
],
});
console.log(
chalk.green(
"Connected to Composio Googlesheets MCP server, executing task..."
)
);
const result = await agent.executeTask(TASK, {
debugOnAgentOutput: (agentOutput) => {
console.log("\n" + chalk.cyan.bold("===== AGENT OUTPUT ====="));
console.dir(agentOutput, { depth: null, colors: true });
console.log(chalk.cyan.bold("===============") + "\n");
},
onStep: (step) => {
console.log("\n" + chalk.cyan.bold(`===== STEP ${step.idx} =====`));
console.dir(step, { depth: null, colors: true });
console.log(chalk.cyan.bold("===============") + "\n");
},
});
await agent.closeAgent();
console.log(chalk.green.bold("\nResult:"));
console.log(chalk.white(result.output));
return result;
} catch (error) {
console.error(chalk.red.bold("Error creating agent or executing task:"));
console.error(
chalk.red(error instanceof Error ? error.stack : String(error))
);
}
}
(async () => {
try {
if (process.argv.length < 3) {
console.error(
chalk.red("Error: Please provide your MCP URL as an argument")
);
process.exit(1);
}
await run(process.argv[2]);
} catch (error) {
console.error(chalk.red("Error:"), error);
process.exit(1);
}
})();