MTG卡牌管理服务器

基于Swift实现的魔法风云会(MTG)卡牌管理服务器,提供牌组管理和卡牌搜索功能。

  • by ericraio
  • Swift
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 ericraio/mtg-mcp

使用说明

MTG MCP服务器

Magic的Swift实现:收集(MTG)模型上下文协议(MCP)服务器,通过MCP协议提供卡组管理和卡搜索功能。

概述

本项目提供两台MCP服务器:

  1. MTG甲板经理 (mtg-mcp)-游戏状态管理,包括牌组加载、抽牌、mulligans和侧板
  2. Scryfall API服务器 (scryfall-mcp)-使用Scryfall API检索卡信息

这两台服务器都实现了MCP协议,以便与Claude Desktop等MCP兼容客户端无缝集成。

特性

MTG甲板经理

  • 分段装药:以各种格式解析和加载甲板列表
  • 游戏状态管理:轨道甲板、手和侧板内容物
  • 卡片绘制:用适当的洗牌从牌堆中抽出牌
  • Mulligan支持:伦敦mulligan实施
  • Sideboad:在主牌组/餐具柜和手牌之间交换卡片
  • 游戏重置:重置为初始游戏状态
  • 统计:实时甲板和手牌统计

Scryfall API服务器

  • 卡片搜索:使用Scryfall查询语法进行高级卡片搜索
  • 随机卡片:使用可选过滤器生成随机卡
  • 名字查找:通过精确或模糊的名称匹配查找卡片
  • 丰富的卡数据:完整的卡片信息,包括图片、价格和合法性

安装

先决条件

  • Swift 6.2或更高版本
  • macOS 13或更高版本

从源代码构建

# Clone the repository
git clone <repository-url>
cd mtg-mcp

# Build the project
swift build

# Run tests
swift test

安装可执行文件

# Build in release mode
swift build -c release

# Install to local bin (optional)
cp .build/release/mtg-mcp /usr/local/bin/
cp .build/release/scryfall-mcp /usr/local/bin/

用法

运行服务器

MTG甲板管理服务器

# Run with stdio transport (for MCP integration)
swift run mtg-mcp --transport stdio

# Run standalone for testing
swift run mtg-mcp --transport stdio --verbose

Scryfall API服务器

# Run with stdio transport
swift run scryfall-mcp --transport stdio

# Run with debug output
swift run scryfall-mcp --transport stdio --verbose

MCP集成

Claude桌面配置

将服务器添加到Claude Desktop配置文件中。配置文件通常位于:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • 视窗: %APPDATA%\Claude\claude_desktop_config.json
选项1:使用Swift Run(开发)
{
  "mcpServers": {
    "mtg-deck-manager": {
      "command": "swift",
      "args": ["run", "mtg-mcp", "--transport", "stdio"],
      "cwd": "/path/to/mtg-mcp"
    },
    "scryfall-api": {
      "command": "swift", 
      "args": ["run", "scryfall-mcp", "--transport", "stdio"],
      "cwd": "/path/to/mtg-mcp"
    },
    "rules-splitter": {
      "command": "swift",
      "args": ["run", "rules-splitter"],
      "cwd": "/path/to/mtg-mcp"
    }
  }
}
选项2:使用内置可执行文件(生产)
{
  "mcpServers": {
    "mtg-deck-manager": {
      "command": "/path/to/.build/release/mtg-mcp",
      "args": ["--transport", "stdio"]
    },
    "scryfall-api": {
      "command": "/path/to/.build/release/scryfall-mcp", 
      "args": ["--transport", "stdio"]
    }
  }
}
完整配置示例
{
  "mcpServers": {
    "mtg-deck-manager": {
      "command": "swift",
      "args": ["run", "mtg-mcp", "--transport", "stdio"],
      "cwd": "/Users/ericraio/mcp/mtg-mcp"
    },
    "scryfall-api": {
      "command": "swift", 
      "args": ["run", "scryfall-mcp", "--transport", "stdio"],
      "cwd": "/Users/ericraio/mcp/mtg-mcp"
    }
  }
}
生产配置(推荐)

首先,构建可执行文件:

cd /Users/ericraio/mcp/mtg-mcp
swift build -c release

然后使用此配置:

{
  "mcpServers": {
    "mtg-deck-manager": {
      "command": "/Users/ericraio/mcp/mtg-mcp/.build/release/mtg-mcp",
      "args": ["--transport", "stdio"]
    },
    "scryfall-api": {
      "command": "/Users/ericraio/mcp/mtg-mcp/.build/release/scryfall-mcp",
      "args": ["--transport", "stdio"]
    }
  }
}

重要提示:

  • 这 cwd 路径必须指向 /Users/ericraio/mcp/mtg-mcp Package.swift存在的位置
  • 生产构建比开发构建更快、更可靠
  • 更新配置后重新启动Claude Desktop

验证设置

添加配置后:

  1. 重新启动克劳德桌面
  2. 开始新的对话
  3. 测试集成 请克劳德:
    • “显示可用的MTG工具”
    • “加载一个简单的MTG牌组并抽一些牌”
    • “使用Scryfall搜索闪电螺栓”
    • “查阅MTG规则100.1”

工具使用示例

连接后,您可以通过MCP客户端使用这些工具:

甲板基本管理
# Load a deck
upload_deck with deck_list: "
Deck
4 Lightning Bolt
4 Counterspell  
4 Island
4 Mountain
44 Basic lands

Sideboard
2 Negate
1 Spell Pierce
"

# Draw opening hand
draw_card with count: 7

# View your hand
view_hand

# Get deck statistics
view_deck_stats

# Play a card
play_card with card_name: "Lightning Bolt"

# Mulligan to 6 cards
mulligan with new_hand_size: 6
规则查找和学习
# Look up specific rules
lookup_rule with rule_number: "100.1"
lookup_rule with rule_number: "601.2a"

# Search for rules about concepts
search_rules with concept: "mulligan"
search_rules with concept: "combat"
search_rules with keywords: "cast spell"

# Get comprehensive explanations
explain_concept with concept: "priority"
explain_concept with concept: "triggered abilities"
explain_concept with concept: "stack"
卡片搜索和信息
# Search for cards
search_cards with query: "c:red cmc<=3 t:instant"
search_cards with query: "t:creature pow>=4"

# Get random cards
get_random_card
get_random_card with query: "c:blue t:counterspell"

# Look up specific cards
lookup_card_exact with exact: "Lightning Bolt"
lookup_card_fuzzy with fuzzy: "Jace Mind Sculptor"
高级游戏场景
# Sideboarding
sideboard_swap with remove_card: "Lightning Bolt" and add_card: "Negate"

# Reset game state
reset_game

# Commander deck setup
upload_deck with deck_list: "
Commander
1 Atraxa, Praetors' Voice

Deck  
1 Sol Ring
1 Command Tower
98 Forest
"

克劳德提示示例

以下是配置MCP服务器后,您可以在Claude Desktop中使用的示例提示:

🎮 游戏管理提示

“帮我测试一下我的燃烧甲板”

Load this burn deck and simulate an opening hand:

4 Lightning Bolt
4 Lava Spike  
4 Monastery Swiftspear
4 Eidolon of the Great Revel
20 Mountain
4 Fireblast

Sideboard:
3 Smash to Smithereens
2 Pyroblast

Then draw 7 cards and tell me what kind of opening hand I got.

“模拟穆里根决策”

I want to practice mulligan decisions. Load a competitive deck, draw a 7-card opening hand, and tell me if you think I should keep it or mulligan based on the cards drawn.

📚 规则学习提示

“教我关于堆栈的事”

I'm confused about how the stack works in Magic. Can you:
1. Look up the official rules about the stack
2. Explain how priority works with it
3. Give me an example of how spells and abilities resolve

“战斗伤害规则”

Look up the official MTG rules about combat damage and explain:
- When damage is dealt
- How first strike works
- What happens with deathtouch
- How trample calculates excess damage

🔍 卡片研究提示

“为我的组合牌寻找卡片”

I'm building an artifact combo deck. Search for:
1. Red instants that cost 3 mana or less
2. Artifacts that produce mana
3. Cards that let me draw cards when artifacts enter

Show me the most relevant options.

“随机甲板灵感”

Give me 5 random cards and help me brainstorm a deck theme that could use all of them together.

🏆 学习与策略提示

“规则在考验我”

Quiz me on MTG rules! Look up a random rule number and ask me to explain what it means, then show me the official rule text to check my answer.

“甲板分析”

Load this deck list and analyze it:
- What's the mana curve?
- What's the game plan?
- What rules should I know for the key cards?
- What are potential weaknesses?

[paste deck list here]

甲板格式支持

deck解析器支持多种格式:

标准格式

Deck
4 Lightning Bolt
4 Counterspell
20 Island

Sideboard
2 Negate
1 Spell Pierce

指挥官格式

Commander
1 Atraxa, Praetors' Voice

Deck
1 Sol Ring
1 Command Tower
98 Forest

设置信息

Deck
4 Lightning Bolt (LEA) 
4 Counterspell [7ED]
20 Island

无头格式

4 Lightning Bolt
4 Counterspell  
20 Island

可用的MCP工具

MTG甲板管理工具

工具 说明 参数
upload_deck 加载甲板清单 deck_list:包含甲板列表的字符串
draw_card 从牌组中抽牌 count:要抽的牌数(默认值:1)
view_hand 获取当前手牌内容 无
view_deck_stats 获取甲板统计信息 无
play_card 用手打牌 card_name:要玩的牌的名称
mulligan Mulligan到新的手部尺寸 new_hand_size:新手的尺寸(可选)
sideboard_swap 用餐具柜交换卡片 remove_card:要删除的卡
add_card:要添加的卡
reset_game 重置游戏状态 无
lookup_rule 查找特定的MTG规则 rule_number:规则编号(例如“100”、“601.2a”)
search_rules 搜索MTG规则 keywords:要搜索的关键字(可选)
concept:游戏概念(可选)
explain_concept 获取全面的规则解释 concept:游戏概念解释

Scryfall API工具

工具 说明 参数
search_cards 搜索卡片 query:Scryfall搜索查询
page:页码(可选)
get_random_card 获取随机卡 query:筛选查询(可选)
lookup_card_exact 按确切名称查找卡片 exact:准确的卡名
lookup_card_fuzzy 通过模糊名称查找卡片 fuzzy:近似卡名

规则拆分工具

工具 说明 参数
rules-splitter 下载MTG规则并将其拆分为多个部分 url:自定义规则URL(可选)

发展

项目结构

mtg-mcp/
├── Sources/
│   ├── mtg-mcp/           # MTG Deck Manager server
│   │   └── mtg_mcp.swift  # Main server with rules integration
│   ├── scryfall-mcp/      # Scryfall API server
│   │   └── scryfall_mcp.swift
│   ├── rules-splitter/    # Rules processing tool
│   │   └── main.swift     # Rule file splitter CLI
│   ├── MTGModels/         # Shared models
│   │   ├── Card.swift
│   │   ├── GameState.swift
│   │   ├── ManaCost.swift
│   │   └── Rarity.swift
│   └── MTGServices/       # Shared services
│       ├── DeckParser.swift
│       └── RulesService.swift  # NEW: Rules lookup service
├── Tests/
│   └── mtg-mcpTests/      # Comprehensive test suite
│       ├── CardModelTests.swift
│       ├── DeckParserTests.swift
│       ├── GameStateTests.swift
│       ├── IntegrationTests.swift
│       ├── TestDataLoader.swift
│       └── TestData/      # Sample deck files
├── rules/                 # Generated MTG rules (144 files)
│   ├── 100_general.md
│   ├── 601_casting_spells.md
│   └── ... (142 more rule files)
└── Package.swift

依赖项

  • MCP Swift SDK (0.9.0+)-MCP协议实现
  • SwiftGzip 的 -对Scryfall API的压缩支持
  • ArgumentParser -命令行参数解析

运行测试

# Run all tests
swift test

# Run specific test suite
swift test --filter CardModelTests

# Run tests with verbose output
swift test --verbose

代码的风格

该项目遵循Swift的最佳实践:

  • Swift actors用于线程安全游戏状态管理
  • 并发访问的可发送协议一致性
  • 使用结果类型进行全面的错误处理
  • 异步/等待模式贯穿始终
  • 覆盖率>90%的单元测试

建筑

线程安全

游戏状态使用Swift的 actor 系统,确保跨并发MCP工具调用对可变状态的线程安全访问。

错误处理

使用Swift的全面错误处理 Result 针对不同故障场景的类型和自定义错误类型。

MCP集成

两台服务器都使用官方Swift SDK实现MCP协议,提供:

  • 工具注册和发现
  • 请求/响应处理
  • 传输抽象(stdio、HTTP)
  • 正确的错误传播

api参考

卡片模型

public struct Card: Identifiable, Equatable, Hashable, Codable, Sendable {
    public let id: String
    public var name: String
    public var manaCostString: String
    public var kind: CardKind
    public var rarity: Rarity
    public var typeLine: String
    public var oracleText: String
    public var power: String?
    public var toughness: String?
    public var loyalty: String?
}

游戏状态演员

public actor GameState {
    public func loadDeck(_ deckData: DeckData) async
    public func drawCards(count: Int) async -> [Card]
    public func playCard(named cardName: String) async -> Card?
    public func mulligan(newHandSize: Int) async -> [Card]
    public func sideboardSwap(removeCard: String, addCard: String) async -> (removed: Card?, added: Card?)
    public func resetGame() async
    public func getDeckStats() async -> DeckStats
    public func getHandContents() async -> [String: Int]
}

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 进行更改
  4. 添加新功能的测试
  5. 确保所有测试通过
  6. 提交拉取请求

许可证

本项目按原样提供,用于教育和发展目的。

致谢

  • MCP协议 -用于标准化的AI集成协议
  • API公司 -获取全面的MTG卡数据
  • 敏捷地主 -供参考MTG模型和游戏逻辑
  • MCP Swift SDK -对于Swift MCP的官方实现