Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere Programmierung(d+019) OpenGL(20.09.2026 um 15:51 Uhr)
Sichere ProgrammierungYou Released an App. Now What?(20.09.2026 um 15:52 Uhr)
Sichere Programmierung(d+023) Triangle(20.09.2026 um 15:53 Uhr)
Sichere ProgrammierungHow many coding agents are you using for the same project?(20.09.2026 um 15:57 Uhr)
Sichere ProgrammierungCapyToolkit: 45+ free browser tools, each with a how-to guide(20.09.2026 um 16:00 Uhr)
Sichere ProgrammierungDay-01: Starting My Cybersecurity Journey(20.09.2026 um 16:02 Uhr)
Sichere ProgrammierungWhat crt.sh's Error Pages Taught Me About Retry Logic(20.09.2026 um 16:03 Uhr)
Sichere ProgrammierungI taught my shell to stop me *before* I run `rm -rf /`(20.09.2026 um 16:09 Uhr)
Sichere ProgrammierungTraditional Coding vs Agentic Coding: The Flow State Problem(20.09.2026 um 16:19 Uhr)
Sichere Programmierung(d+019) OpenGL(20.09.2026 um 15:51 Uhr)
Sichere ProgrammierungYou Released an App. Now What?(20.09.2026 um 15:52 Uhr)
Sichere Programmierung(d+023) Triangle(20.09.2026 um 15:53 Uhr)
Sichere ProgrammierungHow many coding agents are you using for the same project?(20.09.2026 um 15:57 Uhr)
Sichere ProgrammierungCapyToolkit: 45+ free browser tools, each with a how-to guide(20.09.2026 um 16:00 Uhr)
Sichere ProgrammierungDay-01: Starting My Cybersecurity Journey(20.09.2026 um 16:02 Uhr)
Sichere ProgrammierungWhat crt.sh's Error Pages Taught Me About Retry Logic(20.09.2026 um 16:03 Uhr)
Sichere ProgrammierungI taught my shell to stop me *before* I run `rm -rf /`(20.09.2026 um 16:09 Uhr)
Sichere ProgrammierungTraditional Coding vs Agentic Coding: The Flow State Problem(20.09.2026 um 16:19 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Como construir um conector MCP com TypeScript e Binance usando arquitetura hexagonal 🛠️

Reagiere als Erste:r — dein Feedback zählt!

Modelos de linguagem como o GPT e o Claude estão revolucionando a forma como interagimos com sistemas digitais. Porém, para que eles sejam realmente úteis em contextos mais práticos, precisamos conectá-los a dados e serviços externos. É aí que entra o Model Context Protocol (MCP), criado pela Anthropic.

Neste artigo, vamos mostrar como construir um conector MCP com TypeScript e a API pública da Binance, aplicando arquitetura hexagonal, validação com Zod e boas práticas modernas de desenvolvimento. Bora ver como isso tudo se conecta? 🚀

O que é o Model Context Protocol (MCP)? 🤖

O MCP é um padrão aberto de comunicação baseado em JSON-RPC 2.0 que permite que LLMs interajam com ferramentas externas de forma segura, padronizada e reutilizável.

Imagine poder disponibilizar sua API como um "plugin nativo" para um modelo de IA — sem reinventar a roda. Com o MCP, isso fica simples.

Acesse a documentação oficial aqui: modelcontextprotocol.io

Arquitetura hexagonal na prática 🧱

Utilizamos a arquitetura hexagonal (também conhecida como ports & adapters) para garantir desacoplamento e testabilidade. Essa abordagem organiza nosso código em:

  • Domínio: regras de negócio puras, como cálculos de tendência.
  • Ports: contratos que o domínio espera para acessar dados externos.
  • Adapters: implementações que atendem essas portas, como chamadas à API da Binance.
  • Aplicação: onde orquestramos os métodos MCP.
  • Interface Primária: o servidor MCP que expõe os métodos.

Estrutura sugerida:

/src
  /domain
    /ports
      BinanceDataPort.ts
    MarketAnalysis.ts
  /adapters
    /secondary
      BinanceServiceAdapter.ts
    /primary
      server.ts
  /application
    /methods
      binanceMethods.ts

Implementando o domínio 📈

// domain/MarketAnalysis.ts
import { BinanceDataPort } from './ports/BinanceDataPort';

export class SimpleMarketAnalysis {
  constructor(private readonly binancePort: BinanceDataPort) {}

  async analyze(symbol: string, interval: string, limit: number) {
    const prices = await this.binancePort.getCandles(symbol, interval, limit);
    const shortSMA = this.calculateSMA(prices, 10);
    const longSMA = this.calculateSMA(prices, 20);
    const signal = shortSMA > longSMA ? 'buy' : shortSMA < longSMA ? 'sell' : 'neutral';
    return { shortSMA, longSMA, signal };
  }

  private calculateSMA(prices: number[], period: number): number {
    if (prices.length < period) throw new Error('Not enough data for SMA');
    return prices.slice(-period).reduce((acc, val) => acc + val, 0) / period;
  }
}

Criando a port (interface) 🔌

// domain/ports/BinanceDataPort.ts
export interface BinanceDataPort {
  getCandles(symbol: string, interval: string, limit: number): Promise<number[]>;
  getTickerPrice(symbol: string): Promise<{ symbol: string; price: string }>;
  getOrderBook(symbol: string, limit?: number): Promise<{ lastUpdateId: number; bids: [string, string][]; asks: [string, string][] }>;
  getRecentTrades(symbol: string, limit?: number): Promise<Array<{
    id: number;
    price: string;
    qty: string;
    quoteQty: string;
    time: number;
    isBuyerMaker: boolean;
    isBestMatch: boolean;
  }>>;
}

Adapter: conectando com a Binance 🌐

// adapters/secondary/BinanceServiceAdapter.ts
import axios from 'axios';
import { BinanceDataPort } from '../../domain/ports/BinanceDataPort';

export class BinanceServiceAdapter implements BinanceDataPort {
  async getCandles(symbol: string, interval: string, limit: number) {
    const response = await axios.get('https://api.binance.com/api/v3/klines', {
      params: { symbol, interval, limit },
    });
    return response.data.map((c: any[]) => parseFloat(c[4]));
  }

  async getTickerPrice(symbol: string) {
    const response = await axios.get('https://api.binance.com/api/v3/ticker/price', {
      params: { symbol },
    });
    return response.data;
  }

  async getOrderBook(symbol: string, limit = 100) {
    const response = await axios.get('https://api.binance.com/api/v3/depth', {
      params: { symbol, limit },
    });
    return response.data;
  }

  async getRecentTrades(symbol: string, limit = 50) {
    const response = await axios.get('https://api.binance.com/api/v3/trades', {
      params: { symbol, limit },
    });
    return response.data;
  }
}

Expondo via MCP com Zod 🧠

// application/methods/binanceMethods.ts
import { z } from 'zod';
import { ToolMethodDefinition } from '@modelcontextprotocol/sdk';
import { BinanceServiceAdapter } from '../../adapters/secondary/BinanceServiceAdapter';
import { SimpleMarketAnalysis } from '../../domain/MarketAnalysis';

const binanceAdapter = new BinanceServiceAdapter();
const analyzer = new SimpleMarketAnalysis(binanceAdapter);

export const analyzeTrend: ToolMethodDefinition = {
  description: 'Performs a basic trend analysis using moving averages.',
  inputSchema: z.object({
    symbol: z.string(),
    interval: z.string().default('1h'),
    limit: z.number().min(20).max(1000).default(100),
  }),
  outputSchema: z.object({
    shortSMA: z.number(),
    longSMA: z.number(),
    signal: z.enum(['buy', 'sell', 'neutral']),
  }),
  handler: async ({ symbol, interval, limit }) => {
    return analyzer.analyze(symbol, interval, limit);
  },
};

Subindo o servidor MCP 🚀

// adapters/primary/server.ts
import { createToolServer } from '@modelcontextprotocol/sdk';
import { analyzeTrend } from '../../application/methods/binanceMethods';

createToolServer({
  tools: [
    {
      name: 'binance',
      description: 'Binance API wrapper with trend analysis.',
      methods: { analyzeTrend },
    },
  ],
  port: 3000,
});

Conclusão ✅

Com o MCP e a arquitetura hexagonal, você consegue criar integrações limpas, reutilizáveis e prontas pra escalar. O segredo está em separar responsabilidades e confiar nas boas práticas.

Esse conector com a Binance é só o começo: dá pra plugar qualquer API, incluir mais ferramentas ou até montar uma suíte de análise com múltiplos indicadores.

Se curtiu o conteúdo, deixa aquele like, salva pra consultar depois e compartilha com a galera dev. Até a próxima! 👋

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Como construir um conector MCP com TypeScript e Binance usando arquitetura hexagonal 🛠️

Thematisch verwandte Begriffe: Como, construir, conector, TypeScript · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93956 | A flaw has been found in olivier-ls PHP-FTS up to 1.1.2. Affected by thi…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick