IntegrationsMCP Server
TypeScript SDK
Use the MCP SDK to integrate ConnectSafely into your TypeScript applications
Use the MCP SDK to integrate this server into your TypeScript applications.
Installation
Install the official MCP SDKs using npm:
npm install @modelcontextprotocol/sdkTypeScript SDK
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
// Construct server URL with authentication
const url = new URL("https://mcp.connectsafely.ai/")
url.searchParams.set("apiKey", "YOUR_API_KEY")
const serverUrl = url.toString()
const transport = new StreamableHTTPClientTransport(serverUrl)
// Create MCP client
const client = new Client({
name: "My App",
version: "1.0.0"
})
await client.connect(transport)
// List available tools
const tools = await client.listTools()
console.log(`Available tools: ${tools.map(t => t.name).join(", ")}`)Getting Your API Key
Get your API key from the MCP Setup page in your ConnectSafely dashboard.
Your MCP server URL format:
https://mcp.connectsafely.ai/?apiKey=YOUR_API_KEYUsage Examples
Listing Available Tools
// List all available tools
const tools = await client.listTools()
console.log("Available tools:")
tools.forEach(tool => {
console.log(`- ${tool.name}: ${tool.description}`)
})Calling MCP Tools
// Call a specific tool
const result = await client.callTool({
name: "get_linkedin_comments",
arguments: {
postId: "your-post-id"
}
})
console.log("Tool result:", result)Error Handling
try {
const tools = await client.listTools()
console.log("Connected successfully!")
} catch (error) {
console.error("Failed to connect to MCP server:", error)
}Complete Example
Here's a complete example of a TypeScript application using ConnectSafely MCP server:
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
async function main() {
// Construct server URL with authentication
const url = new URL("https://mcp.connectsafely.ai/")
url.searchParams.set("apiKey", process.env.CONNECTSAFELY_API_KEY || "YOUR_API_KEY")
const serverUrl = url.toString()
const transport = new StreamableHTTPClientTransport(serverUrl)
// Create MCP client
const client = new Client({
name: "LinkedIn Automation App",
version: "1.0.0"
})
try {
// Connect to the server
await client.connect(transport)
console.log("Connected to ConnectSafely MCP server")
// List available tools
const tools = await client.listTools()
console.log(`\nAvailable tools (${tools.length}):`)
tools.forEach(tool => {
console.log(`- ${tool.name}: ${tool.description}`)
})
// Example: Get LinkedIn comments
const comments = await client.callTool({
name: "get_linkedin_comments",
arguments: {
postId: "your-post-id",
limit: 10
}
})
console.log("\nRecent comments:", comments)
} catch (error) {
console.error("Error:", error)
} finally {
// Clean up connection
await client.close()
}
}
main()Environment Variables
For better security, use environment variables to store your API key:
// Load from environment variable
const apiKey = process.env.CONNECTSAFELY_API_KEY
if (!apiKey) {
throw new Error("CONNECTSAFELY_API_KEY environment variable is required")
}
const url = new URL("https://mcp.connectsafely.ai/")
url.searchParams.set("apiKey", apiKey)Then set the environment variable:
export CONNECTSAFELY_API_KEY=your_api_key_hereOr in your .env file:
CONNECTSAFELY_API_KEY=your_api_key_hereSecurity Best Practices
⚠️ Security Warning
Never commit your API keys to version control or expose them in client-side code.
Best Practices:
- Use environment variables for API keys
- Add
.envto your.gitignore - Use different API keys for development and production
- Regenerate keys if accidentally exposed
- Monitor API usage regularly
Troubleshooting
Connection Failed
- Verify your API key is correct
- Ensure you have an active Pro or Agency subscription
- Check your network connection
TypeScript Compilation Errors
Make sure you're using ES modules in your tsconfig.json:
{
"compilerOptions": {
"module": "ES2022",
"moduleResolution": "node",
"target": "ES2022"
}
}