gejyn14/kiwoom_mcp
3.3
If you are the rightful owner of kiwoom_mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
Kiwoom MCP Server is a modular and extensible server designed to interact with the Kiwoom Open API using a RESTful approach.
Tools
7
Resources
0
Prompts
0
Kiwoom MCP Server
키움 OPEN API(REST)를 활용한 .
🏗️ Project Structure
kiwoom_mcp/ # Project root
├── __init__.py # Package initialization
├── main.py # Entry point (clean & simple)
├── server.py # Main MCP server class
├── pyproject.toml # Project configuration
├── README.md # This file
├── config/ # Configuration management
│ ├── __init__.py
│ ├── constants.py # API constants and mappings
│ └── settings.py # Configuration classes
├── models/ # Data models and types
│ ├── __init__.py
│ ├── exceptions.py # Custom exceptions
│ └── types.py # Request/Response models
├── kiwoom/ # Kiwoom API client
│ ├── __init__.py
│ └── client.py # HTTP client for Kiwoom API
├── handlers/ # MCP tool handlers
│ ├── __init__.py
│ ├── base.py # Base handler class
│ ├── auth.py # Authentication handlers
│ └── orders.py # Order management handlers
└── utils/ # Utilities and helpers
├── __init__.py
├── datetime_utils.py # Date/time utilities
└── logging.py # Logging configuration
🚀 특징징
- 모듈러 아키텍처: Clean separation of concerns
- Type Safety: Full TypeScript-like typing with Python
- Error Handling: Comprehensive exception handling
- Configuration: Environment-based configuration
- Logging: Structured logging with multiple levels
- Extensible: Easy to add new features and handlers
📦 Available Tools
Authentication
set_credentials- Set API credentialsget_access_token- Get access token from Kiwoomset_access_token- Set access token directlycheck_token_status- Check token status and expiration
Trading
stock_buy_order- Place buy ordersstock_sell_order- Place sell ordersget_trade_types- Get available trade types
🔧 Configuration
Environment Variables
# Kiwoom API Configuration
KIWOOM_APPKEY=your_app_key
KIWOOM_SECRETKEY=your_secret_key
KIWOOM_IS_MOCK=false
KIWOOM_ACCESS_TOKEN=your_token
KIWOOM_TOKEN_EXPIRES_DT=20241231235959
# Server Configuration
MCP_SERVER_NAME=kiwoom-stock-mcp
MCP_SERVER_VERSION=1.0.0
LOG_LEVEL=INFO
Programmatic Configuration
from config.settings import KiwoomConfig, ServerConfig
# Create configurations
kiwoom_config = KiwoomConfig(
appkey="your_app_key",
secretkey="your_secret_key",
is_mock=False
)
server_config = ServerConfig(
name="custom-server-name",
version="1.0.0",
log_level="DEBUG"
)
🏃 Running the Server
Basic Usage
python main.py
With Environment Variables
export KIWOOM_APPKEY=your_app_key
export KIWOOM_SECRETKEY=your_secret_key
export KIWOOM_IS_MOCK=true
python main.py
🔌 Extending the Server
Adding New Handlers
- Create a new handler in
handlers/:
# handlers/portfolio.py
from .base import BaseHandler
from ..models.types import PortfolioRequest, PortfolioResponse
class PortfolioHandler(BaseHandler):
async def get_portfolio(self, arguments: Dict[str, Any]) -> List[types.TextContent]:
# Implementation here
return self.create_success_response("Portfolio retrieved")
- Register in
server.py:
# In KiwoomMCPServer.__init__()
self.portfolio_handler = PortfolioHandler(self.kiwoom_config)
# In _setup_handlers()
elif name == "get_portfolio":
return await self.portfolio_handler.get_portfolio(arguments)
Adding New API Endpoints
- Add constants in
config/constants.py:
ENDPOINTS = {
"TOKEN": "/oauth2/token",
"STOCK_ORDER": "/api/dostk/ordr",
"PORTFOLIO": "/api/portfolio", # New endpoint
}
- Extend the client in
kiwoom/client.py:
def get_portfolio(self, access_token: str) -> PortfolioResponse:
# Implementation here
pass
Adding New Models
- Define in
models/types.py:
@dataclass
class PortfolioRequest:
account_number: str
include_positions: bool = True
@dataclass
class PortfolioResponse:
success: bool
positions: List[Position]
total_value: float
🔍 Benefits of This Structure
- Maintainability: Each component has a single responsibility
- Testability: Easy to unit test individual components
- Scalability: Simple to add new features without affecting existing code
- Reusability: Components can be reused across different parts
- Type Safety: Full typing for better IDE support and error catching
- Configuration Management: Centralized and flexible configuration
- Error Handling: Consistent error handling across all components
🚦 Migration from Monolithic Structure
The old 489-line main.py has been refactored into:
- 12 focused modules with clear responsibilities
- Type-safe interfaces between components
- Proper separation of configuration, business logic, and presentation
- Extensible architecture for future enhancements
This structure follows Python best practices and makes the codebase much more professional and maintainable.