MCP Code Examples
Full examples using the official MCP SDKs to connect and use tools.
Python MCP Client
Using the official
mcp Python SDK.pip install
pip install mcppython - main.py
import asyncio
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
API_KEY = "YOUR_API_KEY"
MCP_URL = "https://api.xobni.ai/mcp/"
async def main():
headers = {"Authorization": f"Bearer {API_KEY}"}
async with streamablehttp_client(MCP_URL, headers=headers) as (read, write, _):
async with ClientSession(read, write) as session:
await session.initialize()
# List available tools
tools = await session.list_tools()
print("Available tools:")
for t in tools.tools:
print(f" - {t.name}: {t.description[:60]}...")
# Get agent info
result = await session.call_tool("get_agent_info", {})
print(f"\nAgent: {result}")
# Read inbox (latest 5 emails)
result = await session.call_tool("read_inbox", {"limit": 5})
print(f"\nInbox: {result}")
# Send an email
result = await session.call_tool("send_email", {
"to": ["friend@example.com"],
"subject": "Hello from my AI agent!",
"body_text": "This email was sent via MCP through Xobni.ai."
})
print(f"\nSent: {result}")
# Search emails semantically
result = await session.call_tool("search_emails", {
"query": "meeting next week",
"limit": 5
})
print(f"\nSearch results: {result}")
# Store a document
result = await session.call_tool("store_document", {
"collection": "contacts",
"data": {"name": "Jane Doe", "email": "jane@example.com"},
"metadata": {"source": "conference"}
})
print(f"\nStored: {result}")
# Ask AI about stored documents (RAG)
result = await session.call_tool("ask_storage", {
"question": "Who did we meet at the conference?",
"collection": "contacts"
})
print(f"\nAI Answer: {result}")
# Calendar: create an event
result = await session.call_tool("create_calendar_event", {
"title": "Team Standup",
"start_time": "2026-03-20T10:00:00",
"end_time": "2026-03-20T10:30:00",
"timezone": "America/New_York",
})
print(f"\nCreated event: {result}")
# Schedule an email
result = await session.call_tool("schedule_email", {
"send_at": "2026-03-20T09:00:00Z",
"to": ["recipient@example.com"],
"subject": "Reminder",
"body_text": "Don't forget the meeting today!",
})
print(f"\nScheduled email: {result}")
asyncio.run(main())TypeScript / Node.js
Using the
@modelcontextprotocol/sdk package.npm install
npm install @modelcontextprotocol/sdktypescript - index.ts
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
const API_KEY = "YOUR_API_KEY";
async function main() {
const transport = new StreamableHTTPClientTransport(
new URL("https://api.xobni.ai/mcp/"),
{ requestInit: { headers: { Authorization: `Bearer ${API_KEY}` } } }
);
const client = new Client({ name: "my-app", version: "1.0.0" });
await client.connect(transport);
// List tools
const { tools } = await client.listTools();
console.log("Tools:", tools.map(t => t.name));
// Read inbox
const inbox = await client.callTool({
name: "read_inbox",
arguments: { limit: 5 }
});
console.log("Inbox:", inbox);
// Search emails
const search = await client.callTool({
name: "search_emails",
arguments: { query: "project update", limit: 3 }
});
console.log("Search:", search);
// Store a document
const stored = await client.callTool({
name: "store_document",
arguments: {
collection: "notes",
data: { title: "Meeting Notes", content: "Discussed Q2 roadmap" }
}
});
console.log("Stored:", stored);
// Ask AI about stored documents (RAG)
const answer = await client.callTool({
name: "ask_storage",
arguments: { question: "What was discussed in meetings?" }
});
console.log("AI Answer:", answer);
// Calendar: create an event
const event = await client.callTool({
name: "create_calendar_event",
arguments: {
title: "Team Standup",
start_time: "2026-03-20T10:00:00",
end_time: "2026-03-20T10:30:00",
timezone: "America/New_York",
}
});
console.log("Created event:", event);
// Schedule an email
const scheduled = await client.callTool({
name: "schedule_email",
arguments: {
send_at: "2026-03-20T09:00:00Z",
to: ["recipient@example.com"],
subject: "Reminder",
body_text: "Don't forget the meeting today!",
}
});
console.log("Scheduled email:", scheduled);
await client.close();
}
main();