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 henry@mcphub.com.
Kiwoom MCP Server is a modular and extensible server designed to interact with the Kiwoom Open API using a RESTful approach.
Tools
set_credentials
Set API credentials
get_access_token
Get access token from Kiwoom
set_access_token
Set access token directly
check_token_status
Check token status and expiration
stock_buy_order
Place buy orders
stock_sell_order
Place sell orders
get_trade_types
Get available trade types
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.